views:

303

answers:

1

For a UIPickerView, when do you use a plist for the datasource? For example, if I just wanted the Array 1-100, is that something that should be in a plist, or something that should be created in viewDidLoad?

Also, I notice in the UIPickerView in the Clock app on the iPhone, the numbers move, but the words hours and seconds stay the same. How do I do that? Thanks!

A: 

Firstly, the choice between using a plist and an array/dictionary is really just a preference. The plist is good for a visual way to view and edit the data, but ultimately you will have to convert it into an NSDictionary before you can use it in your code:

NSString *path = [[NSBundle mainBundle] pathForResource:@"myplist" orType:@"plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithContentsOfFile:path];

Personally, I would do this in an initialisation method such as awakeFromNib, initWithNibName or init, rather than viewDidLoad.

Finally, the way to get different columns on the picker is to set the number of components. This is done in the UIPickerViewDataSource method

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

which is required. It's the same as having to tell your UITableView how many rows and sections it will have. Simply supply the info for each component in the UIPickerViewDelegate method

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

where you can give different data to separate components.

imnk