Introduction Picker can be configured to display one dail or many. By defults, pickers display lists of text, but they can also be made to display images.
Types of Pickers
UIDatePicker
UIPickerView
Delegates & Datasource (The controller view containing date piciker need not conform to UIPickerViewDelegate and UIPickerViewDataSource)
UIPickerViewDelegate
Set the visible data of current row pickerView:titleForRow:forComponent:
...
UIPickerViewDataSource
Set component count (Column count) numberOfComponentsInPickerView:
Set the count of rows of current component pickerView:numberOfRowsInComponent:
Dependent Component The data in some components depends on other component. For example, picker view contains 2 components, the first component is used to display proviences name, and the second component is used to display cities under the selected provience.
Use NSDictionary to hold data #define COMPONENT_PROVIENCE = 0; #defien COMPONENT_CITY = 1;
Override pickerView:didSelectRow:inComponent:, then set the correct data array, finally, call reloadComponent: to reset - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (COMPONENT_PROVIENCE == component)
{
NSString *pProvience = [pProviences objectAtIndex:row]: self.pCities = [pProvienceCity objectForKey:pProvience]; [pPickerView selectRow:0 inComponent:COMPONENT_CITY animated:YES]; [pPickerView reloadComponent:COMPONENT_CITY];
} }
....
Sample Code
Return data in selected row
NSArray *pDrrayData;
IBOutlet UIPcikerView *pPickerView;
...
//Get the selected data in the first column, if you want to get //data in the third column, just replace 0 with 2
NSInteger row = [pPickerView selectedRowInComponent:0];
NSString *pData = pDrrayData[row];
Set data to picker Suppose defined following fields: #define COMPONENT_NAME = 0; #defien COMPONENT_NUM = 1;