开发者

Can we enable looping of rows in a UIPickerView as we do it in UIDatePicker? [duplicate]

开发者 https://www.devze.com 2022-12-12 02:33 出处:网络
This question already has answers here: How do you make an UIPickerView component wrap around? (3 answers)
This question already has answers here: How do you make an UIPickerView component wrap around? (3 answers) Closed 9 years ago.

I am developing an App where i am struck with the looping of rows in a UIPickerView. Can anyone please help me? it would be of great help if anyone would post the s开发者_如何学Goolution. I want the rows in a UIPickerView scroll continuosly in a circular manner without having a end point.


It is possible. For anyone of you who needs it, try this.


I don't think it's possible. I've heard of people repeating the list of values a large number of times, and starting the user off somewhere in the middle.


It does work, just watch the memory. Told that items not shown are not stored so can make list huge. Check with profiler if worried. 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. use NSIntegerMax, if memory problem, use less say 2000
    return 2000;
}

(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]; // or your strings (this is for double.  numberItems is your list size.
}

(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:3+1000 inComponent:0 animated:NO]; //pick about half the max you made earlier or about 100000 if using NSIntegerMax
    [self.view addSubview:pickerView];
}

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

Jon

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号