我想在UIPickerView组件中显示一组连续的数字,但它像Clock-> Timer应用程序的秒组件一样包围。我可以启用的唯一行为看起来像Timer应用程序的hours组件,您只能在一个方向滚动。

有帮助吗?

解决方案

将行数设置为大数量同样容易,并使其从高值开始,用户很可能在很长一段时间内滚动滚轮 - 即便如此,更糟糕的是,它们会触底。

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    // Near-infinite number of rows.
    return NSIntegerMax;
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    // Row n is same as row (n modulo numberItems).
    return [NSString stringWithFormat:@"%d", row % numberItems];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.pickerView = [[[UIPickerView alloc] initWithFrame:CGRectZero] autorelease];
    // ...set pickerView properties... Look at Apple's UICatalog sample code for a good example.
    // Set current row to a large value (adjusted to current value if needed).
    [pickerView selectRow:currentValue+100000 inComponent:0 animated:NO];
    [self.view addSubview:pickerView];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSInteger actualRow = row % numberItems;
    // ...
}

其他提示

我在这里找到了答案:

http://forums.macrumors.com/showthread.php p = 6120638&安培;突出= UIPickerView#post6120638

当它要求行的标题时,请给它: 代码:

return [rows objectAtIndex:(row % [rows count])];

当它说用户didSelectRow:inComponent:时,请使用以下内容:

代码:

//we want the selection to always be in the SECOND set (so that it looks like it has stuff before and after)
if (row < [rows count] || row >= (2 * [rows count]) ) {
    row = row % [rows count];
    row += [rows count];
    [pickerView selectRow:row inComponent:component animated:NO];
}

看起来UIPickerView不支持原生包装,但你可以通过插入更多数据集来愚弄它,当选择器停止时,将组件居中到数据集的中间。

只需多次创建一个数组,这样就可以多次输入数字。让我们说当想要从0到23的数字并将其放在一个数组中。我们会像这样做10次......

NSString *stdStepper;

    for (int j = 0; j<10; j++) {
        for(int i=0; i<24; i++)
        {
            stdStepper = [NSString stringWithFormat:@"%d", i];

            [_hoursArray addObject:stdStepper];

        }
    }

稍后我们将行0设置为像这样

[_hoursPickerView selectRow:120 inComponent:0 animated:NO];

所有答案都没有真正让选择器视图循环滚动。

我基于 UIScrollView 创建了一个循环tableView。基于这个tableView,我重新实现了 UIPickerView 。您可能对此 DLPickerView 感兴趣。此选择器视图具有 UIPickerView 所具有的所有功能,但也提供了许多新功能,并且自定义此选择器视图更加容易。

https://github.com/danleechina/DLPickerView

请注意,此 DLPickerView 循环滚动实际上是循环滚动。所有的魔法都是因为另一个类 DLTableView

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top