标签:
Picker View控件: 中文:单滚轮选择器 自定义选择器需要遵守两个协议:数据源协议和委托协议 UIPickerView有一个实例方法selectRow:(NSInteger)inComponent:(NSInteger)animated:(BOOL),以编程的方式来选择值。 UIPickerView必须用代码实现一些协议才能正常显示,而且无法在Attributes Inspector(属性检查)中配置选择器视图的外观 1. 数据源协议 数据源协议(UIPickerViewDataSource)用来描述选择器显示多少信息。 有下面两个主要方法: numberOfComponentInPickerView -- 返回选择器的组件数 pickerView:numberOfRowsInComponent -- 返回指定组件包含多少行(不同的输入值) 例如,如果要创建一个选择器,显示两列,其中第一列包含一个可供选择的值,而第二列包含两个,则代码实现如下: @interface ViewController : UIViewController <UIPickerViewDataSource> - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 2; //显示两列 } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if(component==0) { //如果是第一列,显示1个标签 return 1; } else { //否则显示2个标签 return 2; } } 2. 委托协议 委托协议(UIPickerViewDelegate)负责创建和使用选择器的系列工作。它负责将合适的数据传递给选择器进行显示,并确定用户是否做出了选择。 该协议包含多个方法,但只有两个是必不可少的: pickerView:titleForRow:forComponent -- 根据指定的组件和行号返回该行的标题,即应向用户显示的字符串 没有实现这方法的话选择器标题将不显示,运行后会以?代替。 pickerView:didSelectRow:inComponent -- 当用户在选择器中做出选择时,将调用该委托方法,并向它传递用户 选择的行号以及用户最后触摸的组件。 UIPickView还有一个实例方法- (NSInteger)selectedRowInComponent:(NSInteger)component,用来返回指定列当前选择的行数。通常和pickerView:didSelectRow:inComponent配合使用。 想象这样的情况:选择器有两列,第一列是国家,第二列是地区。假如你先转动了国家,这个时候你想得到默认选择的地区,就只能通过selectedRowInComponent来取得。(这个时候didSelectRow得到的row值,是国家那一列的) @interface ViewController : UIViewController <UIPickerViewDataSource,UIPickerViewDelegate> - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if(component==0) { //如果是第一列,显示"中国" return @"中国"; } else { if(row==0) { return @"北京"; } else { return @"香港"; } } } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if(component==0) { NSLog(@"用户选择了第一列"); } else { NSLog(@"用户选择了第二列"); NSLog(@"用户选择了第%d行",row); } } 还有一些非必要,但是也很有用的方法: pickerView:rowHeightForComponent -- 给指定组件返回(设置)行高,单位为像素 pickerView:widthForComponent -- 给指定组件返回(设置)宽度,单位为像素 pickerView:viewForRow:viewForComponent:ReusingView -- 给指定组件和行号返回相应位置应显示的自定义视图,优先级高于pickerView:titleForRow:forComponent - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { if(component==0) { //设置第一列的行高 return 50; } else { return 30; } } - (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component { if(component==0) { //设置第一列的宽度 return 100; } else { return 80; } } - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { if(component==0) { //第一列返回一个Label组件 UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 75, 32)]; label.backgroundColor = [UIColor greenColor]; label.textColor = [UIColor redColor]; label.text = @"First"; return label; } else { //第二列返回一个图片组件 if(row==0) { return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Orange.gif"]]; } else { return [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Strawberry.gif"]]; } } }
标签:
原文地址:http://www.cnblogs.com/pocket-mood/p/4331282.html