代码实现UIPickerView
先说一下当个组件选取器,我们创建一个数组NSAray来保存选取器中的内容;选取器本身不会储存任何数据,,它通过调用数据源和委托方法来显示数据;但是对于大量数据的数据源,数组并不合适,我们可以做一个静态列表如plist文件或者URL载入,和后面将讲在文件中获取数据,还以多个选取器的之间的关联如何实现;先说下简单的单个选取器:
先把效果图贴出来
1.新建工程名为PickerViewDemo , File->New->Project ->single View Application -> next
2.在视图上添加选取器
pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)]; // 指定Delegate pickerView.delegate=self; // 显示选中框 pickerView.showsSelectionIndicator=YES; [self.view addSubview:pickerView];以上可以在视图显示一个选取器,但是内容空白,pickerView.showsSelectionIndicator=YES;是这只当前选取器所选中的内容:
选取器上显示数据,必须依赖两个协议,UIPickerViewDelegate和UIPickerViewDataSource,把他们添加到ViewController.h文件中
#import3.然后在.m文件的ViewDidLoad中初始化界面@interface ViewController : UIViewController { UIPickerView *pickerView; NSArray *pickerData; } @end
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)]; // 指定Delegate pickerView.delegate=self; // 显示选中框 pickerView.showsSelectionIndicator=YES; [self.view addSubview:pickerView]; NSArray *dataArray = [[NSArray alloc]initWithObjects:@"许嵩",@"周杰伦",@"梁静茹",@"许飞",@"凤凰传奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永传", nil]; pickerData=dataArray; // 添加按钮 CGRect frame = CGRectMake(120, 250, 80, 40); UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect]; selectButton.frame=frame; [selectButton setTitle:@"SELECT" forState:UIControlStateNormal]; [selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:selectButton]; }
4.实现UIPickerView的代理方法,将数据显示在选取器上所需要几个方法
#pragma mark - #pragma mark Picker Date Source Methods //返回显示的列数 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } //返回当前列显示的行数 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [pickerData count]; } #pragma mark Picker Delegate Methods //返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上 -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { return [pickerData objectAtIndex:row]; }前两个是数据源的代理方法,一个是返回列,有几个选取器就返回几,第二个是设置选取器有多少行,因为就这一个选取器,所以直接返回行数,即数组元素个数多少;第三个代理方法是将数组元素添加到了选取器上面显示;
说一下两个协议实例方法,参考http://www.cnblogs.com/edsioon/
UIPickerViewDelegate中的实例方法
-(void) pickerView: (UIPickerView *)pickerView didSelectRow: (NSInteger)row inComponent: (NSInteger)component
作用: 当用户选择某个row时,picker view调用此函数
参数: pickerView representing the picker view request the data
-(CGFloat) pickerView:(UIPickerView *)pickerView rowHeightForComponent: (NSInteger) component
作用:由picker view调用,当其在绘制row内容,需要row的高度时
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger) component
作用: 当picker view需要给指定的component.row指定title时,调用此函数
-(UIView *)pickerView: (UIPickerView *)pickerView view ForRow:(NSInteger) row forComponent:(NSInteger) component reusingView:(UIView *) view
作用: 当picker view需要给指定的component.row指定view时,调用此函数.返回值为用作row内容的view
参数: view参数, a view object that was previously used for this rows, but is now hidden and cached by the picker view
- (CGFloat)pickerView: (UIPickerView *)pickerView widthForComponent:(NSInteger) component
作用:当picker view 需要row的宽度时,调用此函数
UIPickerViewDataSource中的实例方法
按照官方文档的说法,UIPickerViewDataSource这个协议仅有的功能就是提供picker view中component的个数和各个component中的row的个数,虽然名为datasource,但是它工作于MVC的C中
本协议仅有两个实例方法,均需要实现
-(NSInteger) numberOfComponentsInPickerView:(UIPickerView *)pickerView
作用:返回pickerView应该有几个component
-(NSInteger) pickerView:(UIPickerView *) pickerView numberOfRowsInComponent:(NSInteger) component
作用:返回指定component应该有几个row
5.关于按钮响应事件,关于按钮的形成和添加响应事件不再提,前面都有,
-(void) buttonPressed:(id)sender { NSInteger row =[pickerView selectedRowInComponent:0]; NSString *selected = [pickerData objectAtIndex:row]; NSString *message = [[NSString alloc] initWithFormat:@"你选择的是:%@",selected]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; }
[pickerViewselectedRowInComponent:0];方法返回当前被选中的索引序号,这是UIPickerView的实例方法,在官方文档中
UIPickerView还有其他实例方法
- (NSInteger) numberOfRowsInComponent:(NSInteger)component
参数为component的序号(从左到右,以0起始),返回指定的component中row的个数
-(void) reloadAllComponents
调用此方法使得PickerView向delegate: Query for new data for all components
-(void) reloadComponent: (NSInteger) component
参数为需更新的component的序号,调用此方法使得PickerView向其delegate: Query for new data
-(CGSize) rowSizeForComponent: (NSInteger) component
参数为component的序号,返回值为the size of rows in the given components, picker view 通过调用委托方法中的pickerView:widthForComponent:和pickerView:rowHeightForComponent:获得返回值
-(NSInteger) selectedRowInComponent: (NSInteger) component
参数为component的序号,返回被选中row的序号,若无row被选中,则返回-1
-(void) selectRow: (NSInteger)row inComponent: (NSInteger)component animated: (BOOL)animated
作用:在代码指定要选择的某component的某row
参数:row序号,component序号,BOOL值(若为YES,转动spin到你选择的新值,若为NO,直接显示你选择的值)
-(UIView *) viewForRow: (NSInteger)row forComponent: (NSInteger)component
参数:row序号,component序号,返回由委托方法pickerView:viewForRow:forComponentreusingView:指定的view.如果委托对象并没有实现这个方法,或者说这个view并不是可见的,则返回nil
附上源代码: http://download.csdn.net/detail/duxinfeng2010/4410909