Reusing View in UIpickerview with iOS 7

Reusing view in UIPickerView with iOS 7

I have reported my problem to Apple Technical Support. They confirm the bug :

There does indeed seem to be a difference between the behavior in iOS 6 and 7.

They ask me to report a bug on the bugreport tool. Now, just wait for the fix :)

How do I reuse a view inside UIPickerView in this case?

The view is never not nil.

Correct. The docs are wrong. There is actually no such thing as reusing views in a picker view viewForRow. You always need to make a new view and return it.

Reused view in UIPickerVIew viewForRow: loses its subviews?

I'm unable to duplicate your problem. I made a simple project with the following code, which is very similar to yours. I added a frame to the label, and colored the background so I could see what was going on. Can you see any difference from what you have that would cause a difference in outcome (maybe the problem is in the "..." area that you don't show)?

- (void)viewDidLoad {
[super viewDidLoad];
self.picker.dataSource = self;
self.picker.delegate = self;
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 10;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel* label = nil;
if (view == nil) {
view = [[UIView alloc] init];
view.backgroundColor = [UIColor blueColor];
label = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 60, 22)];
label.text = @"test";
[view addSubview:label];
}
if (label == nil) {
NSLog(@"%@",view.subviews);
label = view.subviews[0]; // I do get subviews here.
}
return view;
}

Reuse the same pickerview for different textfields in tableview

You can simply reload the picker view:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
[Yourpickerview reloadAllComponents];
}


Related Topics



Leave a reply



Submit