views:

19

answers:

1

I am just learning how to setup a multiple picker and I am a little curious about the two labeled sections below:

 // METHOD_001
 NSInteger row_001 = [doublePicker selectedRowInComponent:0];
 NSInteger row_002 = [doublePicker selectedRowInComponent:1];
 NSString *selected_001 = [pickerData_001 objectAtIndex:row_001];
 NSString *selected_002 = [pickerData_002 objectAtIndex:row_002];

EDIT_001:

Maybe if I simplify this a little .... I know what METHOD_001 is doing, but what about the method below. If I comment it out my code still runs but the picker UI does not get populated with my data, so its obviously involved in taking data from the dataSource so the picker can display it. One puzzling aspect is that it uses "objectAtIndex:row" to access an item, even though the whole UI needs populating (i.e. all the rows) Does this then mean that this gets called for each row, seems funny that it does not take a NSArray nd populated the picker UI in one shot?

// DELEGATE
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if(component == kCoffeeIndex) return [pickerData_001 objectAtIndex:row];
    return [pickerData_002 objectAtIndex:row];
}

gary

+1  A: 

The delegate method -pickerView:titleForRow:forComponent: will be called every time the UI thinks the data is needed.

It doesn't take an array because the titles may be generated dynamically or retrieved lazily. It's actually the same story for table views. Usually only the visible rows will be loaded, to reduce the data required.

KennyTM
Ah I see, so its called for each visible row when the application loads to populate the UI, sure that makes sense.
fuzzygoat
Perfect Kenny, I put an NSLog in there to print the objectsAtIndex:row to the console and it does just what you said. At startup it loads the visible picker fields, then when you spin the picker it loads any new ones that become visible. Many thanks much appreciated.
fuzzygoat