ios 你如何让 UIPickerView 组件环绕?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/214441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you make an UIPickerView component wrap around?
提问by David
I would like to show a set of consecutive numbers in a UIPickerView component but have it wrap around like the seconds component of the Clock->Timer application. The only behavior I can enable looks like the hours component of the Timer application, where you can scroll in only one direction.
我想在 UIPickerView 组件中显示一组连续的数字,但让它像 Clock->Timer 应用程序的秒组件一样环绕。我可以启用的唯一行为类似于 Timer 应用程序的小时组件,您只能在其中向一个方向滚动。
回答by squelart
It's just as easy to set the number of rows to a large number, and make it start at a high value, there's little chance that the user will ever scroll the wheel for a very long time -- And even then, the worse that will happen is that they'll hit the bottom.
将行数设置为大数并使其从高值开始也很容易,用户滚动滚轮很长时间的可能性很小 - 即便如此,情况会更糟发生的是他们会触底。
- (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;
// ...
}
回答by David
I found my answer here:
我在这里找到了答案:
http://forums.macrumors.com/showthread.php?p=6120638&highlight=UIPickerView#post6120638
http://forums.macrumors.com/showthread.php?p=6120638&highlight=UIPickerView#post6120638
When it asks for the title of a row, give it: Code:
当它询问一行的标题时,给它:代码:
return [rows objectAtIndex:(row % [rows count])];
When it says the user didSelectRow:inComponent:, use something like this:
当它说用户 didSelectRow:inComponent: 时,请使用以下内容:
Code:
代码:
//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];
}
It appears that the UIPickerView does not support wrapping around natively, but you can fool it by inserting more sets of data to be displayed and when the picker stops, centering the component to the middle of the data set.
似乎Uipickerview不支持当本地性周围包装,但是您可以通过插入更多要显示的数据集以及选择器停止,将组件居中到数据集的中间时来欺骗它。
回答by user2912794
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...
只需多次创建一个数组,这样您就可以多次获得您的数字。让我们说何时想要将数字从 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];
}
}
later we set the row 0 selected like this
稍后我们将第 0 行设置为这样选择
[_hoursPickerView selectRow:120 inComponent:0 animated:NO];
回答by Dan Lee
All the answers are not really making picker view scroll cyclically.
所有的答案并没有真正使选择器视图循环滚动。
I have made a cyclic tableView based on UIScrollView
. And based on this tableView, I re-implement UIPickerView
. You might be interested in this DLPickerView
. And this picker view has all the features that UIPickerView
has, but also gives many new features, and custom this picker view is much easier.
我已经基于UIScrollView
. 并基于这个 tableView,我重新实现了UIPickerView
. 您可能对此感兴趣DLPickerView
。并且这个选择器视图拥有所有的功能UIPickerView
,但也提供了许多新功能,自定义这个选择器视图要容易得多。
https://github.com/danleechina/DLPickerView
https://github.com/danleechina/DLPickerView
And be aware of that this DLPickerView
cyclically scroll is really scrolling cyclically. All the magic happened because of another class DLTableView
.
请注意,这种DLPickerView
循环滚动实际上是循环滚动。所有的魔法都是因为另一个班级而发生的DLTableView
。