UIPickerView

一、简介

<

之间的对应关系值。UIDatePicker类使用一个自定义子类的UIPickerView显示日期和时间。为了看一个例子,挖掘在时钟应用程序报警窗格添加("+")按钮

<

<<继承关系:UIPickerView --> UIView -->UIResponder-->NSObject

<

格式为

1--> 设置数据源(属性的作用

[pickerView setDelegate:self];   (这是具体的例子

@property(nullable,nonatomic,weak) id dataSource;// 设置数据源, 默认是空的   (这是属性的说明

二、UIPickerView的属性(属性的顺序与苹果API一致)

1-->设置数据源

 [pickerView setDelegate:self]; 

@property(nullable,nonatomic,weak) id dataSource;// 设置数据源, 默认是空的,弱引用

2-->设置代理

[pickerView setDelegate:self];

@property(nullable,nonatomic,weak) id delegate; //设置代理, 默认是空的,弱引用

3-->设置是否显示UIPickerView中的选中标记,在iOS7之后这个属性没有任何效果

pickerView.showsSelectionIndicator =YES;

@property(nonatomic) BOOL showsSelectionIndicator; // 默认是 NO。该属性控制是否显示UIPickerView中的选中标记(以高亮背景作为选中标记)

4-->获取UIPickerView指定列中包含的列表项的数量

NSInteger row1 =pickerView.numberOfComponents; //获取组键数(列数)

@property(nonatomic,readonly) NSInteger numberOfComponents; /从数据源和委托获取并缓存的信息

三、UIPickerView的属性方法

1-->获取UIPickerView包含的列数量

 NSInteger row2 = [pickerView numberOfRowsInComponent:1];//获取对象下标组键(列数)的行数

- (NSInteger)numberOfRowsInComponent:(NSInteger)component;

2--> 获取UIPickerView包含的指定列中列表项的大小。该方法返回一个CGSize对象。

 NSLog(@"%@", NSStringFromCGSize( [self.pickerView rowSizeForComponent:0])); //打印获取的每列的size;

- (CGSize)rowSizeForComponent:(NSInteger)component;

3--> 返回该UIPickerView指定列的列表项所使用的UIView控件。

UIView *pickview=[self.pickerView viewForRow:0 forComponent:0];

- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;//返回视图提供的委托通过pickerView:viewForRow:forComponent:reusingView:

如果行或者组件不可见或委托没有实现pickerView:viewForRow:forComponent:reusingView,则为nil

4--> 重载所有分区

[pickerView reloadAllComponents];//刷新UIPickerView

- (void)reloadAllComponents;

5--> 重载某一分区

[pickerView reloadComponent:1];//刷新某一列的数据

- (void)reloadComponent:(NSInteger)component;

6--> 设置选中该UIPickerView中指定列的特定列表项

[pickerView pickerView selectRow:2 inComponent:0animated:YES]; //设置选中的第几列第几行 ,程序一开始运行时就选中该列该行

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated; // 将指定的行滚动到中心。该方法设置选中该UIPickerView中指定列的特定列表项。最后一个参数控制是否使用动画。

7--> 返回该UIPickerView指定列中被选中的列表项。

self.row =  [self.pickerView selectedRowInComponent:0];//获取被选中的第一列的行数(这里是为了在给第二列赋值时用的,因此在此之前要理清代理方法的执行顺序,以免造成不必要的bug)

- (NSInteger)selectedRowInComponent:(NSInteger)component; //返回选定行。如果没有选中返回-1

四、UIPickerView的UIPickerViewDataSource

必须实现的方法

1-->返回显示的列数

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView

{

    return1;// 返回1表明该控件只包含1列

}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件包含的列数

2-->显示每组键(列数)的行数

//指定每个表盘上有几行数据

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

{

    NSInteger result = 0;

    switch (component) {

        case 0:

            result = self.letter.count;//根据数组的元素个数返回几行数据

            break;

        case 1:

            result = self.number.count;

            break;


        default:

            break;

    }

    return result;

}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;       //指定每个表盘上有几行数据

五、UIPickerView的UIPickerViewDelegate

可选方法

1-->返回每列的宽度

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

 if (component ==0) {

 return 100;//第一组键(列)的宽度

}else{

 return 100;//第二组键(列)的宽度

    }

}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED;//该方法返回的CGFloat值将作为该UIPickerView控件中指定列的宽度

2-->返回每列的高度

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

 if (component ==0) {

 return 100;//第一组键(列)的高度

}else{

 return 100;//第二组键(列)的高度

    }

}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED;//该方法返回的CGFloat值将作为该UIPickerView控件中指定列中列表项的高度。

3-->设置某一行显示的标题

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

 if (component ==0) {

 return [self.content[row]objectForKey:@"State"];//返回省的名称

}else{

 //下面俩行代码是获得plist文件中城市的名称

 NSArray *cities = [self.content[self.row]objectForKey:@"Cities"];

 NSString *city =[cities[row] objectForKey:@"city"];

return city;

    }

}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED;//该方法返回的UIView控件将直接作为该UIPickerView控件中指定列的指定列表项。

4-->通过属性字符串设置某一行显示的标题

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component{

 NSString *str = [_nameArray objectAtIndex:row];

 NSMutableAttributedString *AttributedString = [[NSMutableAttributedStringalloc]initWithString:str];

[AttributedString addAttributes:@{NSFontAttributeName:[UIFontboldSystemFontOfSize:18], NSForegroundColorAttributeName:[UIColor whiteColor]}range:NSMakeRange(0, [AttributedString length])];

 return AttributedString;

}

- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

<注意>如果以上两个方法都实现了,优先实现attributedTitleForRow的方法

5-->设置某一行显示的view视图

//可以用UILabel 来设置字体大小 和背景颜色 是一个可以自定义设置的方法

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

 UILabel *label;//声明一个UILabel

 if (component == 0) {//当是第一行

 //设置frame

label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 50)];

 //将省名赋给label.text

label.text = [self.content[row]objectForKey:@"State"];

 //改变字体的颜色

label.textColor = [UIColor yellowColor];

 //设置字体大小

label.font = [UIFont systemFontOfSize:20];//用label来设置字体大小

 //改变背景的颜色

        label.backgroundColor = [UIColor redColor];

}else if (component == 1){//当是第二行时


label  = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 100, 50)];


 NSArray *cities = [self.content[self.row]objectForKey:@"Cities"];

 NSString *city =[cities[row] objectForKey:@"city"];


label.text = city ;//将城市名称赋给label.text

label.textColor = [UIColor purpleColor];

label.font = [UIFont systemFontOfSize:20];


        label.backgroundColor = [UIColor redColor];

    }

 return label;


}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view __TVOS_PROHIBITED;//当用户单击选中该UIPickerView控件的指定列的指定列表项时将会激发该方法。

6-->选中某一行时执行的回调

//被选中的第几列 的 第几行

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

 if(component ==0){

 //刷新对应列

        [self.pickerView reloadComponent:1];

 //当选择第一列时 第二列切到第一行

[self.pickerView selectRow:0inComponent:component+1 animated:YES];

    }

}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED;//当用户单击选中该UIPickerView控件的指定列的指定列表项时将会激发该方法

六、UIPickerView的拓展

实际应用中UIPickerView经常和UITextField配合使用,给大家提供一个很方便的封装类YLSOPickerView。

调用方法 

1、导入#import "YLSOPickerView.h"

2、在- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField实现

#pragma mark UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

{

    if (textField.tag==401) {

        YLSOPickerView *picker = [[YLSOPickerView alloc]init];

        picker.array = @[@"中信银行",@"中国银行",@"中国邮政储蓄银行",@"上海浦东发展银行",@"广发银行股份有限公司",@"华夏银行",@"中国农业银行",@"中国工商银行",@"中国建设银行",@"招商银行",@"中国民生银行",@"兴业银行",@"中国光大银行",@"交通银行"];

        picker.title = bank;

        [picker show];

        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(keyboardHide:)];

        //设置成NO表示当前控件响应后会传播到其他控件上,默认为YES。

        tapGestureRecognizer.cancelsTouchesInView = YES;

        //将触摸事件添加到当前view

        [picker addGestureRecognizer:tapGestureRecognizer];

        return NO;


    }

    return YES;

}

3、实现声明手势注销方法

- (void)keyboardHide:(UITapGestureRecognizer *)tap

{

    YLSOPickerView *pickView=(YLSOPickerView *)tap.view;//最好用单例

    [pickView quit];


}

4、-viewDidload里面声明通知

- (void)viewDidLoad

{

  [super viewDidLoad];

  //接受通知

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(getValue:) name:@"value" object:nil];

}

5、实现通知方法

-(void)getValue:(NSNotification *)notification//点击完成输出选中的text

{

self.textFeild.text = notification.object;

    DLog(@"%@", self.textFeild.text);

}

参考:

iOS系统UIPickerView的简单使用和方法属性介绍

UIPickerView

iOS学习 - UIPickerView

iOS选择器视图控件(UIPickerView)使用方法总结

YLSPicker

你可能感兴趣的:(UIPickerView)