问题
I have a simple UI picker view in an iOS app (iPhone) and I'm looking to pre-populate it with a range of numbers on launch. What would be the most pragmatic/quickest/optimized way to populate it? I'm new to iOS development, so I'm just starting to test the waters. The documentation is pretty decent but I'd like to get some insight from seasoned developers on the most effective way to accomplish what I'm doing?
tl;dr
I want to populate a UI picker view with the the number range 45-550 upon the start of my application, what's the best way to do so?
回答1:
Despite the possibility that I may not qualify as a seasoned developer, I would do it this way. In the class that will be the data source and delegate of the picker view:
#define PICKER_MIN 45
#define PICKER_MAX 550
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return (PICKER_MAX-PICKER_MIN+1);
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [NSString stringWithFormat:@"%d", (row+PICKER_MIN)];
}
来源:https://stackoverflow.com/questions/4020081/most-effective-way-to-populate-a-picker-view-with-range-of-integers