edi*_*die 1 iphone objective-c uipickerview
任何人都可以帮助我如何使UIPickerView在最后一行的值之后显示第一行的值,就像UIDatePicker在12之后将跟随1.
谢谢
我认为它不可能真正使它循环,但你可以伪造它.
#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]];
}
Run Code Online (Sandbox Code Playgroud)
你可以改变pickerData中的字符串数量,它仍然可以正常工作.所选行选择最接近中间的"第一"值.row%[pickerData count]返回以下假设pickerData中有3个NSStrings:
ROW RETURNS 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]).如果你有什么不明白的东西或我错过的东西,请评论.希望这有帮助!