UIPickerView 循环滚动(一种假象)

时间:2023-03-10 01:48:49
UIPickerView 循环滚动(一种假象)

因为网上没有查到相关方法,故而采用一种假象的方法来实现,选项循环滚动

- (void)viewDidLoad {

[super viewDidLoad];

/**

UIPickerView 选择器

*/

CGFloat Y = [UIScreen mainScreen].bounds.size.height-120;

CGFloat W = [UIScreen mainScreen].bounds.size.width;

CGFloat H = 120;

UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, Y, W, H)];

pickerView.showsSelectionIndicator = YES;

pickerView.backgroundColor = [UIColor whiteColor];

pickerView.dataSource = self;

pickerView.delegate = self;

[self.view addSubview:pickerView];

//数据源

self.group = @[@[@"1",@"2",@"3"],@[@"11",@"22",@"33",@"44"],@[@"111",@"222",@"333"]];

//设定 默认选中的 一行(比如:1 2 3...6 7 8...10,默认选择在“5”上)

//    [pickerView selectRow:5 inComponent:1 animated:YES];

for (int i = 0; i<self.group.count; i++) {

[pickerView selectRow:150 inComponent:i animated:YES];

}

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {

return self.group.count;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {

NSArray *arr = self.group[component];

return arr.count * 100;

}

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

return 50;

}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{

return 50;

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {

if (component == 1) {

return [self.group[component] objectAtIndex:row % 4];

}else{

return [self.group[component] objectAtIndex:row % 3];

}

}