1. numberOfComponents:返回UIPickerView当前的列数
NSInteger num = _pickerView.numberOfComponents;
NSLog( @"%d", num);
2. - (NSInteger)numberOfRowsInComponent:(NSInteger)component; 返回component列中有多少行。
NSInteger numInCp = [_pickerView numberOfRowsInComponent:0];
NSLog(@"%d",numInCp);
3. - (CGSize)rowSizeForComponent:(NSInteger)component; 返回component中一行的尺寸。
1 CGSize size = [_pickerView rowSizeForComponent:0]; 2 NSLog(@"%@", NSStringFromCGSize(size));
4. delegate:
4.0 设置UIPickerView代理
1 _pickerView.delegate = self;
4.1 // 设置UIPickView每行显示的内容
1 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 2 { 3 return @"showData"; 4 }
4.2
1 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; 2 3 // 返回一个视图,用来设置pickerView的每行显示的内容。 4 -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 5 { 6 UIView *myView=[[UIView alloc]init]; 7 8 myView.backgroundColor = [UIColor redColor]; 9 10 return myView; 11 }
效果:
5. dataSource:数据源
1 #pragma mark - dataSource method 2 // 设置每列显示3行 3 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 4 { 5 return 3; 6 } 7 // 设置显示2列 8 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 9 { 10 return 2; 11 }
6. showsSelectionIndicator:是否显示指示器,默认为NO
1 _pickerView.showsSelectionIndicator = NO;
注意:设置UIPickerView的行数与列数需要设置数据源,遵守UIPickerViewDataSource,设置UIPickerView的内容需要设置代理,并且遵守代理方法UIPickerViewDelegate。
7.当点击UIPickerView的某一列中某一行的时候,就会调用这个方法。
1 -(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
8. 返回第component列每一行的高度
1 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
9.刷新某一列的数据
一旦调用了这个方法,就会重新给数据源发送消息计算这列的行数、重新给代理发送消息获得这列的内容
1 [pickerView reloadComponent:1];
10. 刷新所有列的数据
1 - (void)reloadAllComponents;
11. 返回选中的是第component列的第几行。
1 - (NSInteger)selectedRowInComponent:(NSInteger)component;