Most effective way to populate a picker view with range of integers?

為{幸葍}努か 提交于 2019-12-22 10:28:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!