IPickerView

NS_ASSUME_NONNULL_BEGIN


@protocol UIPickerViewDataSourceUIPickerViewDelegate;


NS_CLASS_AVAILABLE_IOS(2_0) @interface UIPickerView : UIView <NSCodingUITableViewDataSource>


@property(nullable,nonatomic,weakid<UIPickerViewDataSource> dataSource;                // default is nil. weak reference

@property(nullable,nonatomic,weakid<UIPickerViewDelegate>   delegate;                  // default is nil. weak reference

@property(nonatomic)        BOOL                       showsSelectionIndicator;   // default is NO


// info that was fetched and cached from the data source and delegate

@property(nonatomic,readonlyNSInteger numberOfComponents;

- (NSInteger)numberOfRowsInComponent:(NSInteger)component;

- (CGSize)rowSizeForComponent:(NSInteger)component;


// returns the view provided by the delegate via pickerView:viewForRow:forComponent:reusingView:

// or nil if the row/component is not visible or the delegate does not implement 

// pickerView:viewForRow:forComponent:reusingView:

- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;


// Reloading whole view or single component

- (void)reloadAllComponents;

- (void)reloadComponent:(NSInteger)component;


// selection. in this case, it means showing the appropriate row in the middle

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;  // scrolls the specified row to center.


- (NSInteger)selectedRowInComponent:(NSInteger)component;                                   // returns selected row. -1 if nothing selected


@end




@protocol UIPickerViewDataSource<NSObject>

@required


// returns the number of 'columns' to display.

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;


// returns the # of rows in each component..

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

@end



@protocol UIPickerViewDelegate<NSObject>

@optional


// returns width of column and height of row for each component. 

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;


// these methods return either a plain NSString, a NSAttributedString, or a view (e.g UILabel) to display the row for the component.

// for the view versions, we cache any hidden and thus unused views and pass them back for reuse. 

// If you return back a different object, the old one will be released. the view will be centered in the row rect  

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;

- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0); // attributed title is favored if both methods are implemented

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view;


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;


@end


NS_ASSUME_NONNULL_END




选取器被用来取代PC上面的下拉菜单,它是一个大大的滚轮,它占用固定的大小 320×216.

 

一、创建

大小虽然固定,但是位置可以任意(不过横向被充满,咱也只能改变纵向位置)。与UItableView 类似,UIpickerView 类也需要一个数据源。与表格视图不同的是,选取器不使用索引路径,而是用一个NSinteger 值来标识每一行。选取器可以有多个表盘,每个都可以,作为一个组件引用。

 

选取器视图使用代理作为数据源,因此数据源可以存在于另一个类或者视图控制器中。

C代码   收藏代码
  1. UIPickerView* pickerView = [ [ UIpickerView alloc] initWithFrame:CGRectmake(0.0,100.0,0.0,0.0)];    
  2. pickerView.delegate = self;    
  3. pickerView.dataSource =  self;    
 

二、属性

恶心的苹果把许多属性保留为私有的,开放给开发者的少的可怜,可怜到唯一可用的外观选项就是选择窗口。

将选取器的 showsSelectionIndicator 属性设置为YES,可以在当前选择上显示一个透明窗口:

C代码   收藏代码
  1. pickerView.showsSelectionIndicator = YES;   
 

三、数据源

创建好界面就要考虑数据源了。比必须实现下列数据源委托方法,这些方法是 UIPickerViewDataSource 协议的必要方法。

 

numberOfComponentsInPickerView  

滚轮数量

 

numberOfRowsInComponent   

为选取器中每个滚轮设置不同数目的行。这个方法返回指定滚轮的行数。

除此之外,UIPickerViewDelegate 协议还实现了下列方法,来获取选取器组件特定的信息。

 

titleForRow 

 返回对应滚轮(组件)的给定行的实际表盘取值。会以 NSString 对象返回这些值。

 

viewForRow

这个方法可以重写选取器显示组件表盘默认行为,令任何 UIView 类都可以在其中显示。

 

widthForComponet

返回给定组件(滚轮)的宽度。如果这方法未实现,选取器会自动调整到合适的宽度。

 

rowHeightComponent

返回给定组件(滚轮)的高度。如果这个方法未实现,选取器会自动调整到合适的高度。

 

四、显示

C代码   收藏代码
  1. [self.view addSubview:pickerView ];  
 

五、读取选取器

使用视图的selectedRowInComponent 方法,是获得选取器视图被选中列的索引的最直接的方法:

C代码   收藏代码
  1. int selectedRow = [ pickerView  selectedRowInComponent:0 ];  
   

 也有一个委托方法,当用户选择了选取器中一行时,会收到通知。用这个方法可以向对象发出警报,这样它就可以对新行做出反应:

C代码   收藏代码
  1. - (void)pickerView:(UIPickerView*)pickerView didSelectedRow:(NSInteger)row inComponent:(NSInteger)component{    
  2. /*添加代码,根据选中行尽享相应操作*/    
  3. }    


  先说一下当个组件选取器,我们创建一个数组NSAray来保存选取器中的内容;选取器本身不会储存任何数据,,它通过调用数据源和委托方法来显示数据;但是对于大量数据的数据源,数组并不合适,我们可以做一个静态列表如plist文件或者URL载入,和后面将讲在文件中获取数据,还以多个选取器的之间的关联如何实现;先说下简单的单个选取器:

先把效果图贴出来


1.新建工程名为PickerViewDemo , File->New->Project ->single View Application -> next


2.在视图上添加选取器

[cpp]  view plain copy
  1. pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];  
  2. //    指定Delegate  
  3.     pickerView.delegate=self;  
  4. //    显示选中框  
  5.     pickerView.showsSelectionIndicator=YES;  
  6.     [self.view addSubview:pickerView];   
以上可以在视图显示一个选取器,但是内容空白,pickerView.showsSelectionIndicator=YES;是这只当前选取器所选中的内容:

选取器上显示数据,必须依赖两个协议,UIPickerViewDelegate和UIPickerViewDataSource,把他们添加到ViewController.h文件中

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>  
  4. {  
  5.   
  6.     UIPickerView *pickerView;  
  7.     NSArray *pickerData;  
  8.   
  9. }  
  10.   
  11. @end  
3.然后在.m文件的ViewDidLoad中初始化界面

[cpp]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.       
  6.     pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 216)];  
  7. //    指定Delegate  
  8.     pickerView.delegate=self;  
  9. //    显示选中框  
  10.     pickerView.showsSelectionIndicator=YES;  
  11.     [self.view addSubview:pickerView];   
  12.       
  13.     NSArray *dataArray = [[NSArray alloc]initWithObjects:@"许嵩",@"周杰伦",@"梁静茹",@"许飞",@"凤凰传奇",@"阿杜",@"方大同",@"林俊杰",@"胡夏",@"邱永传", nil];  
  14.       
  15.     pickerData=dataArray;  
  16.       
  17. //     添加按钮     
  18.     CGRect frame = CGRectMake(120, 250, 80, 40);  
  19.     UIButton *selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  20.     selectButton.frame=frame;  
  21.     [selectButton setTitle:@"SELECT" forState:UIControlStateNormal];  
  22.       
  23.     [selectButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];  
  24.       
  25.     [self.view addSubview:selectButton];  
  26.       
  27. }  

4.实现UIPickerView的代理方法,将数据显示在选取器上所需要几个方法

[cpp]  view plain copy
  1. #pragma mark -  
  2. #pragma mark Picker Date Source Methods  
  3.   
  4. //返回显示的列数  
  5. -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView  
  6. {  
  7.     return 1;  
  8. }  
  9. //返回当前列显示的行数  
  10. -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component  
  11. {  
  12.     return [pickerData count];  
  13. }  
  14.   
  15. #pragma mark Picker Delegate Methods  
  16.   
  17. //返回当前行的内容,此处是将数组中数值添加到滚动的那个显示栏上  
  18. -(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component  
  19. {  
  20.     return [pickerData objectAtIndex:row];  
  21. }  
前两个是数据源的代理方法,一个是返回列,有几个选取器就返回几,第二个是设置选取器有多少行,因为就这一个选取器,所以直接返回行数,即数组元素个数多少;第三个代理方法是将数组元素添加到了选取器上面显示


说一下两个协议实例方法,参考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.关于按钮响应事件,关于按钮的形成和添加响应事件不再提,前面都有,

[cpp]  view plain copy
  1. -(void) buttonPressed:(id)sender  
  2. {  
  3.      NSInteger row =[pickerView selectedRowInComponent:0];  
  4.      NSString *selected = [pickerData objectAtIndex:row];  
  5.      NSString *message = [[NSString alloc] initWithFormat:@"你选择的是:%@",selected];  
  6.       
  7.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示"   
  8.                                                     message:message  
  9.                                                    delegate:self  
  10.                                           cancelButtonTitle:@"OK"  
  11.                                           otherButtonTitles: nil];  
  12.     [alert show];  
  13.       
  14. }  

[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





        先介绍一下我们要实现什么功能。有1个选择器,有左右两个轮子,做轮子选择省份,右轮子选择城市,其中选择省份时,右边的城市会自动更新。


 

1、首先,对UIPickerView绑定Delegate和DataSource到相应的ViewController。此处不再赘述。可以用代码或者Interface界面设置。

2、首先实现数据的初始化。

(1)在.h文件中定义如下变量。其中provinces_cities.plist请见附件。

Cpp代码   收藏代码
  1. @interface IkrboyViewController : UIViewController{  
  2.     NSDictionary *dict;//用于存储省份-城市的数据  
  3.     NSArray *provinceArray;//省份的数组  
  4.     NSArray *cityArray;//城市的数组,在接下来的代码中会有根据省份的选择进行数据更新的操作  
  5. }  

 (2)在.m的viewDidLoad方法中加上初始化数据的处理。具体处理在initPicker方法
 

Cpp代码   收藏代码
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     // Do any additional setup after loading the view, typically from a nib.  
  5.     [self initPicker];  
  6. }  
  7.   
  8. //初始化PickerView使用的数据源  
  9. -(void)initPicker{  
  10.     NSBundle *bundle = [NSBundle mainBundle];  
  11.     NSString *plistPath = [bundle pathForResource:@"provinces_cities" ofType:@"plist"]; 
  12.     dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];  
  13.     provinceArray = [dict allKeys];  
  14.      
  15.     NSInteger selectedProvinceIndex = [self.pickerView selectedRowInComponent:0];  
  16.     NSString *seletedProvince = [provinceArray objectAtIndex:selectedProvinceIndex];  
  17.     cityArray = [dict objectForKey:seletedProvince];  
  18.     NSLog(@"%d",[provinceArray count]);  
  19. }  

 3、将数据绑定到UIPickerView

Cpp代码   收藏代码
  1. //以下3个方法实现PickerView的数据初始化  
  2. //确定picker的轮子个数  
  3. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {  
  4.     return 2;  
  5. }  
  6. //确定picker的每个轮子的item数  
  7. - (NSInteger)pickerView:(UIPickerView *)pickerView  
  8. numberOfRowsInComponent:(NSInteger)component {  
  9.     if (component == 0) {//省份个数  
  10.         return [provinceArray count];  
  11.     } else {//市的个数  
  12.         return [cityArray count];  
  13.     }  
  14. }  
  15. //确定每个轮子的每一项显示什么内容  
  16. #pragma mark 实现协议UIPickerViewDelegate方法  
  17. -(NSString *)pickerView:(UIPickerView *)pickerView  
  18.             titleForRow:(NSInteger)row forComponent:(NSInteger)component {  
  19.     if (component == 0) {//选择省份名  
  20.         return [provinceArray objectAtIndex:row];  
  21.     } else {//选择市名  
  22.         return [cityArray objectAtIndex:row];  
  23.     }  
  24. }  

 4.随时监听UIPickerView的滚动。

Cpp代码   收藏代码
  1. //监听轮子的移动  
  2. - (void)pickerView:(UIPickerView *)pickerView  
  3.       didSelectRow:(NSInteger)row inComponent:(NSInteger)component {  
  4.     if (component == 0) {  
  5.         NSString *seletedProvince = [provinceArray objectAtIndex:row];  
  6.         cityArray = [dict objectForKey:seletedProvince];  
  7.           
  8.         //重点!更新第二个轮子的数据  
  9.          [self.pickerView reloadComponent:1];  
  10.           
  11.         NSInteger selectedCityIndex = [self.pickerView selectedRowInComponent:1];  
  12.         NSString *seletedCity = [cityArray objectAtIndex:selectedCityIndex];  
  13.           
  14.         NSString *msg = [NSString stringWithFormat:@"province=%@,city=%@", seletedProvince,seletedCity];  
  15.         NSLog(@"%@",msg);  
  16.     }  
  17.     else {  
  18.         NSInteger selectedProvinceIndex = [self.pickerView selectedRowInComponent:0];  
  19.         NSString *seletedProvince = [provinceArray objectAtIndex:selectedProvinceIndex];  
  20.           
  21.         NSString *seletedCity = [cityArray objectAtIndex:row];  
  22.         NSString *msg = [NSString stringWithFormat:@"province=%@,city=%@", seletedProvince,seletedCity];  
  23.         NSLog(@"%@",msg);  
  24.     }  
  25. }  

 附加说明:利用下面的代码,获得UIPickerView的不同Component(滚轮)的选中的index。

Cpp代码   收藏代码
  1. NSInteger selectedProvinceIndex = [self.pickerView selectedRowInComponent:0];  
  2. NSInteger selectedCityIndex = [self.pickerView selectedRowInComponent:1];  


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值