NSInteger num = _pickerView.numberOfComponents;
NSLog( @"%d", num);
NSInteger numInCp = [_pickerView numberOfRowsInComponent:0];
NSLog(@"%d",numInCp);
CGSize size = [_pickerView rowSizeForComponent:0];
NSLog(@"%@", NSStringFromCGSize(size));
2.0 设置UIPickerView代理
_pickerView.delegate = self;
// 设置UIPickView每行显示的内容
2.1 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return @"showData";
}
2.2 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
// 返回一个视图,用来设置pickerView的每行显示的内容。
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
UIView *myView=[[UIView alloc]init];
myView.backgroundColor = [UIColor redColor];
return myView;
}
效果:
#pragma mark - dataSource method
// 设置每列显示3行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return 3;
}
// 设置显示2列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
_pickerView.showsSelectionIndicator = NO;
注意:设置UIPickerView的行数与列数需要设置数据源,遵守UIPickerViewDataSource,设置UIPickerView的内容需要设置代理,并且遵守代理方法UIPickerViewDelegate。
当点击UIPickerView的某一列中某一行的时候,就会调用这个方法。
- (CGFloat)pickerView:(UIPickerView *)pickerView
rowHeightForComponent:(NSInteger)component;
一旦调用了这个方法,就会重新给数据源发送消息计算这列的行数、重新给代理发送消息获得这列的内容
[pickerView reloadComponent:1];
- (void)reloadAllComponents;
- (NSInteger)selectedRowInComponent:(NSInteger)component;