I have a UIPickerView with the following delegate methods implementation:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return 2;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
if(!view)
{
view = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 0, 37)] autorelease];
UILabel *label = (UILabel*)view;
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.textAlignment = UITextAlignmentCenter;
label.font = [UIFont boldSystemFontOfSize:24];
label.backgroundColor = [UIColor whiteColor];
view.userInteractionEnabled = NO;
}
((UILabel*)view).text = @"X";
return view;
This code works just fine in iOS 5. That is, I get a UIPickerView with 2 rows with an X on them. Under iOS4, however, even though the 3 methods are being called, when I show the UIPickerView the inside is blank as if no rows were present.
Any ideas?
Well two ideas. First, the view you create has no width – its set to 0. I would imagine you need to give it some width, no?
Second, the description for that delegate method says the incoming view was hidden and cached. That would lead me to believe that you might want to “view.hidden = NO;”.
I’d wager one or both of these is the issue, but YMMI 🙂