UIPickerView 是iOS常用的控件之一,它通过轮转界面提供一系列多值选项,它向用户展示信息,也能收集用户输入。下面是一个普通的UIPickerView控件。
使用UIPickerView控件需要遵守两种协议,一种是UIPickerViewDelegate,另一种是UIPickerViewDataSource。
UIPickerViewDelegate协议的方法有:
1. -(NSString *) pickerView: (UIPickerView * )pickerView
titleForRow: (NSInteger) row
forComponent: (NSInterger) component
该方法根据指定的行号返回该行的标题。
2. -(void) pickerView: (UIPickerView *)pickerView
didSelectRow: (NSInteger)row
inComponent: (NSInteger)component
当用户选择某个row时,picker view调用此函数
3. - (CGFloat) pickerView:(UIPickerView *)pickerView
rowHeightForComponent: (NSInteger) component
由picker view调用,当其在绘制row内容,需要row的高度时
4. - (CGFloat)pickerView: (UIPickerView *)pickerView
widthForComponent:(NSInteger) component
当picker view 需要row的宽度时,调用此函数
5. - (UIView *)pickerView: (UIPickerView *)pickerView
viewForRow:(NSInteger) row
forComponent:(NSInteger) component
reusingView:(UIView *) view
当picker view需要给指定的component.row指定view时,调用此函数.返回值为用作row内容的view
UIPickerViewDataSource协议的方法有:
1. -(NSInteger) numberOfComponentslnPickerView: (UIPickerView *) pickerView
该方法返回UIPickerView需要多少个组件(有多少列)。
2. -(NSInteger) pickerView: (UIPickerView *) pickerView
numberOfRowsInComponet: (NSInteger) component
该方法返回指定组件包含多少行。
常用方法:
1. -
返回指定的component中行数
2. -(void) reloadAllComponents
重新载入所有新数据
3. -(void) reloadComponent: (NSInteger) component
重新载入指定列的数据
4. -(CGSize) rowSizeForComponent: (NSInteger) component
返回值为给定列的行大小, 通过调用委托方法中的pickerView:widthForComponent:和pickerView:rowHeightForComponent:获得返回值
5. -(NSInteger) selectedRowInComponent: (NSInteger) component
返回指定列被选中行的序号,若无row被选中,则返回-1
6. -(void) selectRow: (NSInteger)row
inComponent: (NSInteger)component
animated: (BOOL)animated
在代码指定要选择的某component的某row
参数animated 若为YES,转动spin到你选择的新值;若为NO,直接显示你选择的值
7. -(UIView *) viewForRow: (NSInteger)row
forComponent: (NSInteger)component
返回由委托方法pickerView:viewForRow:forComponentreusingView:指定的view.如果委托方法未实现,或者说这个view并不是可见的,则返回nil
示例代码:
text.h
@interface TBLViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> { NSArray *firstTypes; NSArray *secondTypes; } @property (strong, nonatomic) IBOutlet UIPickerView *pickerView; - (IBAction)Sure:(id)sender; @end
- (void)viewDidLoad<span style="white-space:pre"> </span>// 初始化 { [super viewDidLoad]; firstTypes = [[NSArray alloc] initWithObjects:@"hello", @"hi", @"how are u", nil]; secondTypes = [[NSArray alloc] initWithObjects:@"Zhao", @"Qian", @"Sun", @"Li", @"Zhou", @"Wu", @"Zheng", @"Wang", nil]; pickerView.delegate = self; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (IBAction)Sure:(id)sender {<span style="white-space:pre"> </span>// 显示选中信息 NSInteger first = [pickerView selectedRowInComponent:0];<span style="white-space:pre"> </span>// 选中的行数 NSInteger second = [pickerView selectedRowInComponent:1]; NSString* firstString = [firstTypes objectAtIndex:first];<span style="white-space:pre"> </span>// 选中行的信息 NSString* secondString = [secondTypes objectAtIndex:second]; NSString* message = [NSString stringWithFormat:@"Your chooise is %@ && %@", firstString, secondString]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Chooise Information" message:message delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil]; [alert show]; } #pragma mark - #pragma mark Picker Data Source Methed - (NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView {<span style="white-space:pre"> </span>// 返回列数 return 2; } - (NSInteger) pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{<span style="white-space:pre"> </span>// 返回该列行数 if (component == 0) { return [firstTypes count]; } else{ return [secondTypes count]; } } #pragma mark Picker Delegate Methods - (NSString *) pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{<span style="white-space:pre"> </span>// 返回该行标题 if (component == 0) { return [firstTypes objectAtIndex:row]; } else{ return [secondTypes objectAtIndex:row]; } } @end