41

我想在 UIPickerView 组件中显示一组连续的数字,但让它像 Clock->Timer 应用程序的秒组件一样环绕。我可以启用的唯一行为类似于 Timer 应用程序的小时组件,您只能在一个方向上滚动。

4

3 回答 3

47

将行数设置为大数字同样容易,并使其以较高的值开始,用户滚动滚轮很长时间的可能性很小——即使那样,情况越糟发生的是他们会触底。

- (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;
    // ...
}
于 2008-12-15T03:56:29.493 回答
22

我在这里找到了答案:

http://forums.macrumors.com/showthread.php?p=6120638&highlight=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不支持本地围绕包装,但是您可以通过插入更多要显示的数据集以及选择器停止,将组件居中到数据集的中间时来欺骗它。

于 2008-10-21T13:51:23.853 回答
0

只需多次创建一个数组,这样您就可以多次获得数字。让我们说什么时候想要从 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];
于 2015-05-24T21:45:55.517 回答