UIPickerView无限行 [英] UIPickerView infinite rows

查看:184
本文介绍了UIPickerView无限行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

As seen in UIDatePicker you can scroll up endlessly (as well as down for that matter). I want to accomplish that too, because I want people to select a date such as:

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

1 January, 2010 2 January, 2010 ... 30 December, 2010 31 December, 2010 1 January, 2011 ..

所以它可以永远地继续下去。我怎么做到这一点?因为我只能在委托中给出特定数量的行。

So it can go on forever. How would I accomplish this? Since I can only give a specific amount of rows in the delegate.

推荐答案

我认为你不能真正制作UIPickerView循环,但我过去的方式是从 numberOfRowsInComponent 返回一个非常大的数字,然后计算行的模数以从<返回相应的视图code> viewForRow ,如下所示:

I don't think you can actually make the UIPickerView loop, but the way I've done it in the past is to return a really large number from numberOfRowsInComponent and then calculate the modulus of the row to return the appropriate view from viewForRow, like this:

// 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
}

并在您的初始化中:

- (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];
}

这篇关于UIPickerView无限行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆