iOS学习笔记——UIPickerView的简单应用

来源:互联网 发布:algorithm算法中的sort 编辑:程序博客网 时间:2024/05/21 14:08

今天来学习轮子UIPickerView~~~

依旧打开官方文档……

picker view并没有多余的描述,那么直接来学学怎么应用


版权声明:本文为博主原创文章,欢迎分享学习,转载请务必注明出处!

版权声明:本文为博主原创文章,欢迎分享学习,转载请务必注明出处!

版权声明:本文为博主原创文章,欢迎分享学习,转载请务必注明出处!


1.

Getting the Dimensions of the View Picker 获取PickerView的大小

属性:picker的轮子个数(read only)

@property(nonatomic, readonly) NSInteger numberOfComponents


返回picker每个轮子的item个数

- (NSInteger)numberOfRowsInComponent:(NSInteger)component

返回每一行的大小

A picker view fetches the value of this property by calling the pickerView:widthForComponent: and  pickerView:rowHeightForComponent: delegate methods, and caches it. The default value is (0, 0).

picker view通过呼叫pickerView:widthForComponent: 和 pickerView:rowHeightForComponent:的代理方法来获取这个值,默认值为(0,0);

- (CGSize)rowSizeForComponent:(NSInteger)component


2.

Reloading the View Picker 重新加载picker view

重新加载全部数据

- (void)reloadAllComponents

重新加载某一个轮子的数据

- (void)reloadComponent:(NSInteger)component

3.

Selecting Rows in the View Picker 选择picker view的行


在picker view中选择特定轮子中的一行

- (void)selectRow:(NSInteger)row      inComponent:(NSInteger)component         animated:(BOOL)animated

Parameters

row 行

A zero-indexed number identifying a row of component. 每个轮子的行数从0索引

component组件(轮)

A zero-indexed number identifying a component of the picker view.每个picker view的轮子数从0索引

animated动画

YES to animate the selection by spinning the wheel (component) to the new value; if you specify NO, the new selection is shown immediately.

是否需要动画效果

返回特定轮子的某一行的行数

- (NSInteger)selectedRowInComponent:(NSInteger)component


4.

Returning the View for a Row and Component 

获取使用picker view的指定某个轮子 / 某一行的上一层view 

- (UIView * _Nullable)viewForRow:(NSInteger)row                    forComponent:(NSInteger)component

Return Value

The view provided by the delegate in the pickerView:viewForRow:forComponent:reusingView: method. Returns nil if the specified row of the component is not visible or if the delegate does not implement pickerView:viewForRow:forComponent:reusingView:.

返回值:这个view由代理方法 pickerView:viewForRow:forComponent:reusingView: 提供。如果指定轮子中的指定行是不可见的,或这个代理没有实现pickerView:viewForRow:forComponent:reusingView:. 方法,则返回nil


5.

Specifying the Delegate  

Specifying the Data Source  必须遵从的两个协议!

@protocol UIPickerViewDataSource<NSObject>
//遵从<span style="font-family: Arial, Helvetica, sans-serif;">UIPickerViewDataSource协议必须实现的方法</span>@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


6.

Managing the Appearance of the Picker View 管理picker view的外观

<pre name="code" class="objc">@property(nonatomic) BOOL showsSelectionIndicator

Special Considerations

On iOS 7 and later you cannot customzie the picker view’s selection indicator. The selection indicator is always shown, so setting this property to NO has no effect.

在iOS7之后就弃用了,就不翻译了~


下面来简单应用下:

从城市信息plist文件中获取省份和对应城市,左右两个轮子,左轮子选择省份,右轮子选择城市,其中选择省份时,右边的城市会自动更新。


在interface中声明属性,遵从代理

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>//必须遵从的两个协议@property (strong, nonatomic)UIPickerView *pickerView;  //picker view@property (strong, nonatomic)NSDictionary *dataDict;    //数据字典@property (strong, nonatomic)NSArray *provinceArray;    //存储省份@property (strong, nonatomic)NSArray *cityArray;    //存储城市

- (void)viewDidLoad {    [super viewDidLoad];    [self initPicker];    }- (void)initPicker{    _pickerView = [[UIPickerView alloc] init];        //设置picker view的 dataSource 和 delegate    _pickerView.dataSource = self;    _pickerView.delegate = self;        [self.view addSubview:_pickerView];        //从plist文件中获取数据    NSBundle *bundle = [NSBundle mainBundle];    NSString *plistPath = [bundle pathForResource:@"cities" ofType:@"plist"];    //将pilist中的数据放入字典中    _dataDict = [NSDictionary dictionaryWithContentsOfFile:plistPath];    //省份作为字典的keys    _provinceArray = [_dataDict allKeys];        //获取第0个轮子中的行序号    NSInteger selectedProvinceIndex = [self.pickerView selectedRowInComponent:0];    //获取这一行对应的省份    NSString *selectedProvince = [_provinceArray objectAtIndex:selectedProvinceIndex];    //城市作为该key值的value存进字典里    _cityArray = [_dataDict objectForKey:selectedProvince];    }// returns the number of 'columns' to display.- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    return 2;}// returns the # of rows in each component..- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    if (component == 0) {        return _provinceArray.count;    }else        return _cityArray.count;}//将数据绑定到picker view上- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{    if (component == 0) {        return [_provinceArray objectAtIndex:row];    }else        return [_cityArray objectAtIndex:row];}//监听轮子的滚动- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{    if (component == 0) {        //滚动第0个轮子时,更新第二个轮子的数据        NSString *selectedProvince = [_provinceArray objectAtIndex:row];        _cityArray = [_dataDict objectForKey:selectedProvince];                [self.pickerView reloadComponent:1];            }}


代码例子参考自:http://ikrboy.iteye.com/blog/2003127 谢谢大神的分享指导^_^


版权声明:本文为博主原创文章,欢迎分享学习,转载请务必注明出处!

版权声明:本文为博主原创文章,欢迎分享学习,转载请务必注明出处!

版权声明:本文为博主原创文章,欢迎分享学习,转载请务必注明出处!



0 0
原创粉丝点击