OStack程序员社区-中国程序员成长平台

标题: ios - 如何使用数据快速优化代码和加载 UIPickerView? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:50
标题: ios - 如何使用数据快速优化代码和加载 UIPickerView?

我正在创建包含 2 个组件的 UIPickerView,时间分别为几分钟和几秒钟。我已经在 UI 中创建了选择器并希望用数据填充它,下面是我如何用 0 到 59 的数字填充它的代码。我想让它看起来像圆形,这就是 kSizeOfPicker 为 60000 的原因。当用户按下按钮窗口时选择器出现,但它非常减慢应用程序,因为这 代码在 viewDidLoad 中。我该如何解决?

NSString *stringValue = [[NSString alloc] init];

for(int i=0; i<kSizeOfPicker; i++)
{
    stringValue = [NSString stringWithFormat"%d", i%60];


    [_minutesArray addObject:stringValue];
    [_secondsArray addObject:stringValue];
}

这里是数据源和委托(delegate)方法:

#pragma mark - UIPickerView DataSource Methods


- (NSInteger)numberOfComponentsInPickerViewUIPickerView *)pickerView
{
return 2;
}


- (NSInteger)pickerViewUIPickerView *)pickerView numberOfRowsInComponent :    (NSInteger)component
{

if (component==0)
{
    return [_minutesArray count];
}
else
{
    return [_secondsArray count];
}

}

- (NSString *)pickerViewUIPickerView *)pickerView titleForRowNSInteger)row forComponentNSInteger)component
{
NSString *title;
switch (component)
{

    case 0:
        title = [NSString stringWithFormat"%@ minutes", [_minutesArray objectAtIndex:row]];

        return title;
        break;
    case 1:
        title = [NSString stringWithFormat"%@ seconds", [_secondsArray objectAtIndex:row]];
        return title;
        break;
}
return nil;
}

#pragma mark - UIPickerView Delegate Methods

- (void)pickerViewUIPickerView *)pickerView didSelectRowNSInteger)row inComponentNSInteger)component{

if (component == 0)
    _firstComponent = [_minutesArray objectAtIndex:row];

else
    _secondComponent = [_secondsArray objectAtIndex:row];

}



Best Answer-推荐答案


您不需要准备 NSArray 中的所有数据来归档循环选取器 View 。 以下代码实际上在选定行周围准备了 +-50 个重复数据集。 在 pickerView:didSelectRow:inComponent: 委托(delegate)方法中,您可以重置选定的行。

@interface MyViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (assign, nonatomic) NSUInteger minutes;
@property (assign, nonatomic) NSUInteger seconds;
@end

@implementation MyViewController

#define VALUE_THRESHOLD 60
#define PICKER_DUPLICATES 100
#define CENTER_ROW (VALUE_THRESHOLD * PICKER_DUPLICATES / 2)

- (void)viewDidLoad {
    [super viewDidLoad];
    [_pickerView selectRow:_minutes + CENTER_ROW inComponent:0 animated:NO];
    [_pickerView selectRow:_seconds + CENTER_ROW inComponent:1 animated:NO];
}

- (NSInteger)numberOfComponentsInPickerViewUIPickerView *)pickerView {
    return 2;
}

- (NSInteger)pickerViewUIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return VALUE_THRESHOLD * PICKER_DUPLICATES;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [NSString stringWithFormat"%d", row % VALUE_THRESHOLD];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSInteger actualRow = row % VALUE_THRESHOLD;

    // reset to center dataset
    [pickerView selectRow: actualRow + CENTER_ROW inComponent:component animated:NO];

    // do anything what you want;
    if(component == 0) {
        _minutes = actualRow;
    }
    else {
        _seconds = actualRow;
    }
}

@end

关于ios - 如何使用数据快速优化代码和加载 UIPickerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24795700/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4