I am trying to create a two component UIPickerView, the left side of which holds the values of playing cards (two, three, Jack, etc.) and the right side of which holds images of the four suits. Here is the relevant code:
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
if (component == 0)
return [self.values count];
return [self.column1 count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView
titleForRow:(NSInteger)row
forComponent:(NSInteger)component {
if (component == 0)
return [self.values objectAtIndex:row];
else
return nil;
}
- (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
if(component == 1){
NSString *arrayName = [[NSString alloc] initWithFormat:@"column1"];
NSArray *array = [self valueForKey:arrayName];
return [array objectAtIndex:row];
}
else
return nil;
}
As you can probably see, I have attempted to create a separate picker delegate method for the left component and the right component. However, this is not giving me the intended results. The code seems to only run the viewForRow method, ignoring the other. Thus, I end up with the right component filled with images and the left component empty. Does anybody know of a different way to populate both components?
Probably as soon, as you implement
viewForRow:, it wins overtitleForRow:.Just return an
UILabelwith the needed text. After alltitleForRow:doesn’t do anything else. It’s just a more convinient way, if you ONLY need labels.