我想在UIPickerView组件中显示一组连续的数字,但它像Clock-> Timer应用程序的秒组件一样包围。我可以启用的唯一行为看起来像Timer应用程序的hours组件,您只能在一个方向滚动。
答案 0 :(得分:46)
将行数设置为大数量同样容易,并使其从高值开始,用户很可能在很长一段时间内滚动滚轮 - 即便如此,更糟糕的是,它们会触底。
- (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;
// ...
}
答案 1 :(得分:21)
我在这里找到了答案:
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不支持原生包装,但你可以通过插入更多数据集来愚弄它,当选择器停止时,将组件居中到数据集的中间。
答案 2 :(得分:0)
Just create an array multiple times, so that you have your numbers multiple times. Lets say when want to have the numbers from 0 to 23 and put that in an array. that we will do 10 times like this...
NSString *stdStepper;
for (int j = 0; j<10; j++) {
for(int i=0; i<24; i++)
{
stdStepper = [NSString stringWithFormat:@"%d", i];
[_hoursArray addObject:stdStepper];
}
}
later we set the row 0 selected like this
[_hoursPickerView selectRow:120 inComponent:0 animated:NO];
答案 3 :(得分:0)
所有答案都没有真正让选择器视图循环滚动。
我基于UIScrollView
创建了一个循环表。基于此tableView,我重新实现了UIPickerView
。您可能对此DLPickerView
感兴趣。此选择器视图具有UIPickerView
所具有的所有功能,但也提供了许多新功能,并且自定义此选择器视图更加容易。
https://github.com/danleechina/DLPickerView
请注意,这个DLPickerView
循环滚动实际上是在循环滚动。所有的魔法都是因为另一个班级DLTableView
而发生的。