1

谁能帮助我如何使 UIPickerView 在最后一行的值之后显示第一行的值,就像 UIDatePicker 在 12 之后将跟随 1 一样。

谢谢

4

2 回答 2

5

我认为它不可能真正使它成为圆形,但你可以伪造它。

#define kRowsInPicker 10000

- (void)viewDidLoad {
    NSArray *localPickerData = [[NSArray alloc] initWithObjects:
                                @"Ottawa", @"Toronto", @"Montreal", @"Winnipeg",
                                @"Saskatchewan", @"Iqaluit", @"Edomonton", nil];
    [self setPickerData:localPickerData];
    [localPickerData release];

    UIPickerView *localPicker = [[UIPickerView alloc] initWithFrame:
                                 CGRectMake(0, 0, 320, 216)];
    [localPicker setDelegate:self];
    [localPicker setDataSource:self];
    localPicker.showsSelectionIndicator = YES;
    NSInteger  selectedRow = 0;
    [localPicker selectRow:(((NSInteger)((kRowsInPicker / 2) / [pickerData count])) * [pickerData count]) + (selectedRow%[pickerData count]) inComponent:0 animated:NO];
    [self setPicker:localPicker];
    [localPicker release];

    [[self view] addSubview:picker];
    [super viewDidLoad];
}
#pragma mark -
#pragma mark UIPickerViewDataSource methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView
                                numberOfRowsInComponent:(NSInteger)component {
    // usually "return [pickerData count];"
    return kRowsInPicker; // 10,000 is good, if you add a few more zeros
                          // there seems to be problems.
}

#pragma mark -
#pragma mark UIPickerViewDelegate methods

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [pickerData objectAtIndex:row%[pickerData count]];
}

您可以更改 pickerData 中的字符串数量,它仍然可以正常工作。所选行选择最靠近中间的“第一个”值。假设 pickerData 中有 3 个 NSString,row%[pickerData count] 返回以下内容:

行返回
 0 0
 1 1
 2 2
 3 0
 4 1
 5 2
 6 0
 7 1
 8 2
 ETC...
它只会循环 0 通过数组的长度。viewDidLoad 中令人困惑的东西只是使用您在selectedItem中提供的数组项的索引来选择一个尽可能接近中间的值。没有性能损失或内存泄漏,您分配的只是单个数组。每当您访问他们选择的选项时,请使用 ([localPicker selectedRowInComponent:0]%[pickerData count])。如果有不明白的地方或者我遗漏的地方,欢迎评论。希望这有帮助!

于 2009-08-18T03:42:54.100 回答
0

我制作了一个基于UIScrollView. 并且基于这个tableView,我重新实现了UIPickerView. 您可能对此感兴趣DLPickerView。而且这个picker view拥有了所有的特性UIPickerView,而且还提供了很多新的特性,自定义这个picker view就容易多了。

https://github.com/danleechina/DLPickerView

请注意,这种DLPickerView循环滚动实际上是循环滚动。所有的魔法都是因为另一个班级而发生的DLTableView

于 2016-11-14T06:39:54.113 回答