UIPickerView是很常用的一个UI控件,在各种购物平台选择地址时候都是必备的,下面我们来说一下具体的使用
首先UIPickerView的创建,与多数控件一样,分配内存并设置位置尺寸。
重要的的是代理与数据源,设置代理和数据源后服从代理和数据源协议
<UIPickerViewDelegate,UIPickerViewDataSource>
其中数据源里面有两个必须实现的方法
//设置列数 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return count; } //设置指定列包含的项数 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
}
UIPickerView是可以只设置单列的,列被称为component,此时返回的列数为1即可,单更多时候需要的是多列情况,此时设置返回值为自己所需要的列数即可。
在UIPickerViewDataSource数据源协议中,仅仅提供了UIPickerView包含几列以及每一列的项数,而每一行展示的选项是通过UIPickerViewDelegate协议中的方法来设置的。
//设置每个选项显示的内容 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ } //获取用户当前选中的选项 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ }
UIPickerView基本属性和方法:
设置数据源对象以及代理对象
@property(nullable,nonatomic,weak) id<UIPickerViewDataSource> dataSource; @property(nullable,nonatomic,weak) id<UIPickerViewDelegate> delegate;
重新加载列:
- (void)reloadAllComponents; - (void)reloadComponent:(NSInteger)component;
获取当前选中的选项序号:
- (NSInteger)selectedRowInComponent:(NSInteger)component;
指定选中的项显示在中间位置,一般设置第一项放在中间:
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
下面是一个简单的完整示例:
#import "ViewController.h" @interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource> @property(nonatomic,strong)UIPickerView *pickerVIew; @property(nonatomic,strong)NSDictionary *dictionary; @property(nonatomic,strong)NSArray *provinceArray; @property(nonatomic,copy)NSString *selectedProvince; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.dictionary = @{@"江苏":@[@"南京",@"徐州",@"镇江",@"无锡",@"常州"],@"河北":@[@"石家庄",@"保定",@"承德",@"沧州",@"秦皇岛"]}; //获取字典中所有的省份并排序保存 self.provinceArray = [[self.dictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]; self.selectedProvince = self.provinceArray[0]; [self.view addSubview:self.pickerVIew]; } //懒加载 - (UIPickerView *)pickerVIew{ if (_pickerVIew == nil) { self.pickerVIew = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, 400)]; _pickerVIew.layer.masksToBounds = YES; _pickerVIew.layer.borderWidth = 1; _pickerVIew.delegate = self; _pickerVIew.dataSource = self; } return _pickerVIew; } #pragma mark ------- dateSource&&Delegate -------- //设置列数 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2; } //设置指定列包含的项数 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component == 0) { return self.provinceArray.count; } return [self.dictionary[self.selectedProvince] count]; } //设置每个选项显示的内容 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component == 0) { return self.provinceArray[row]; } return [self.dictionary[self.selectedProvince] objectAtIndex:row]; } //用户进行选择 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == 0) { self.selectedProvince = self.provinceArray[row]; [self.pickerVIew reloadComponent:1]; //设置第二列首选的始终是第一个 [self.pickerVIew selectRow:0 inComponent:1 animated:YES]; } } @end