UIPickerView无限行

Mar*_*ark 5 iphone uipickerview

如在UIDatePicker中所见,您可以无限地向上滚动(以及向下滚动).我也希望实现这一目标,因为我希望人们选择日期,例如:

2010年1月1日2010年1月2日... 2010年12月30日2010年12月31日,2011年1月1日..

所以它可以永远持续下去.我怎么做到这一点?因为我只能在委托中提供特定数量的行.

fil*_*ipe 8

我不认为你实际上可以创建UIPickerView循环,但我过去做的方式是返回一个非常大的数字numberOfRowsInComponent,然后计算行的模数以返回相应的视图viewForRow,如下所示:

// picker data source:
-(NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger) component
{
    // to make it look like the picker is looping indefinetly,
    // we give it a really large length, and then in the picker delegate we consider
    // the row % (actual length) instead of just the row.
    return INT16_MAX;
}


// picker delegate:
-(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *)view
{
    // to make it look like the picker is looping indefinetly,
    // we give it a really large length in the picker data source, and then we consider
    // the row % actual_length instead of just the row.
    row = row % actual_length;
    // where actual length is the number of options that will be looping

    // ... do whatever
}
Run Code Online (Sandbox Code Playgroud)

并在您的初始化:

- (void)viewDidLoad {
    [super viewDidLoad];

    // ... whatever other initialization... 

    // to make it look like the picker is looping indefinetly ,
    // we give it a really large length in the picker data source, and then we consider
    // the row % actual_length instead of just the row, 
    // and we start with the selection right in the middle, rounded to the
    // first multiple of actual_length so we start the selection on 
    // the first option in the list.
    [myPicker selectRow:(INT16_MAX/(2*actual_length))*actual_length inComponent:0 animated:NO];
}
Run Code Online (Sandbox Code Playgroud)


liv*_*ech 2

高级:在委托中指定有限数量的行。然后检测您到达这些行的顶部或底部并更改委托,以便该行实际上位于中间(或顶部,或其他位置)。显然,您需要使用其中一种滚动方法来更改表格的位置(可能没有动画,因此它会在用户不知情的情况下发生)。

合理?我从来没有这样做过,但理论上应该是可行的。