1 1.numberOfComponents:返回UIPickerView当前的列数 2 NSInteger num = _pickerView.numberOfComponents; 3 NSLog( @"%d", num); 4 2. - (NSInteger)numberOfRowsInComponent:(NSInteger)component; 返回component列中有多少行。 5 NSInteger numInCp = [_pickerView numberOfRowsInComponent:0]; 6 NSLog(@"%d",numInCp); 7 3. - (CGSize)rowSizeForComponent:(NSInteger)component; 返回component中一行的尺寸。 8 9 CGSize size = [_pickerView rowSizeForComponent:0]; 10 NSLog(@"%@", NSStringFromCGSize(size)); 11 12 13 delegate: 14 2.0 设置UIPickerView代理 15 _pickerView.delegate = self; 16 // 设置UIPickView每行显示的内容 17 2.1 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 18 { 19 return @"showData"; 20 } 21 22 23 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view; 24 25 // 返回一个视图,用来设置pickerView的每行显示的内容。 26 -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view 27 { 28 UIView *myView=[[UIView alloc]init]; 29 myView.backgroundColor = [UIColor redColor]; 30 return myView; 31 } 32 效果: 33 34 dataSource:数据源 35 #pragma mark - dataSource method 36 // 设置每列显示3行 37 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 38 { 39 return 3; 40 } 41 // 设置显示2列 42 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 43 { 44 return 2; 45 } 46 4. showsSelectionIndicator:是否显示指示器,默认为NO 47 _pickerView.showsSelectionIndicator = NO; 48 49 注意:设置UIPickerView的行数与列数需要设置数据源,遵守UIPickerViewDataSource,设置UIPickerView的内容需要设置代理,并且遵守代理方法UIPickerViewDelegate。 50 51 52 5.-(void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component; 53 当点击UIPickerView的某一列中某一行的时候,就会调用这个方法。 54 6. 返回第component列每一行的高度 55 - (CGFloat)pickerView:(UIPickerView *)pickerView 56 rowHeightForComponent:(NSInteger)component; 57 58 7.刷新某一列的数据 59 一旦调用了这个方法,就会重新给数据源发送消息计算这列的行数、重新给代理发送消息获得这列的内容 60 [pickerView reloadComponent:1]; 61 8. 刷新所有列的数据 62 - (void)reloadAllComponents; 63 9. 返回选中的是第component列的第几行。 64 - (NSInteger)selectedRowInComponent:(NSInteger)component;