万博manbetx客户端UIPickerView –&gt 返回PickView的component列对应的行数。

一、简介

//

<<UIPickerView是一个选择器控件,它比较UIDatePicker更加通用,它可生成单列的选择器,也只是很成多排的选择器,而且开发者完全可由定义选择项之外观,因此用法非常灵活

//  ViewController.m

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

//  UIPickerViewAll

<<UIPickerView直接继承了UIView,没有继承UIControl,因此,它不可知像UIControl那样绑定事件处理方法,UIPickerView的事件处理由该委托对象就。使用UIPickerView的对象应该遵守UIPickerViewDataSource,UIPickerViewDelegate。 

#import “ViewController.h”

格式为

 

1–> 设置数据源(性能的意图

@interface ViewController ()

[pickerView setDelegate:self];   (立马是切实的例证

 

@property(nullable,nonatomic,weak) id dataSource;//
设置数据源, 默认是空的   (立即是性质的辨证

@end

次、UIPickerView的特性(属性的相继和苹果API一致)

 

1–>设置数据源

@implementation ViewController

 [pickerView setDelegate:self]; 

/*

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

 UIPickView控件常用之艺术与性:

2–>设置代理

 (1)  – (NSInteger)numberOfComponentsInPickerView:(UIPickerView
*)pickerView; 返回PickerView的列数

[pickerView setDelegate:self];

 (2)  – (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component;

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

 返回PickView的component列对应的行数

3–>设置是否显示UIPickerView中之当选标记,在iOS7从此这特性没有另外作用

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

pickerView.showsSelectionIndicator =YES;

 返回各国一样列每一行的内容

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

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

4–>获取UIPickerView指定列中涵盖的列表项的数

 用户选中PickView的某部平列和有同实施时见面调用该方法

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

 (5)  – (nullable NSAttributedString *)pickerView:(UIPickerView
*)pickerView attributedTitleForRow:(NSInteger)row
forComponent:(NSInteger)component

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

 修改PickView中component列row行的文件的体裁

其三、UIPickerView的性能方法

 (6)  – (UIView *)pickerView:(UIPickerView *)pickerView
viewForRow:(NSInteger)row forComponent:(NSInteger)component
reusingView:(nullable UIView *)view
该方法返回的UIView的控件将一直作为UIPickView对应的component
列row行的列表项

1–>获取UIPickerView包含的排列数量

 (7)  – (CGFloat)pickerView:(UIPickerView *)pickerView
rowHeightForComponent:(NSInteger)component 设置component列对应之行高

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

 (8)  – (CGFloat)pickerView:(UIPickerView *)pickerView
widthForComponent:(NSInteger)component

– (NSInteger)numberOfRowsInComponent:(NSInteger)component;

 该办法设置component列对应之肥瘦

2–> 获取UIPickerView包含的指定列着列表项的轻重缓急。该办法返回一个CGSize对象。

 (9)  – (void)selectRow:(NSInteger)row inComponent:(NSInteger)component
animated:(BOOL)animated

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

 该方法设置选中的UIPickView
第component列row行项,最后一个参数animated代表是否要用到动画

– (CGSize)rowSizeForComponent:(NSInteger)component;

 (10)  @property(nonatomic,readonly) NSInteger numberOfComponents;

3–> 返回该UIPickerView指定列的列表项所祭的UIView控件。

 获取UIPickerView指定列中含有的列表项的数额,该属性是只有念的

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–> 重载所有分区

//- (void)viewDidLoad

[pickerView reloadAllComponents];//刷新UIPickerView

//{

– (void)reloadAllComponents;

//    [super viewDidLoad];

5–> 重载某平等分区

//

[pickerView reloadComponent:1];//刷新有同列的多少

//    //获取需要展示的数据

– (void)reloadComponent:(NSInteger)component;

//    [self loadData];

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

//    

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

//    // 初始化pickerView

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

//    self.pickerView = [[UIPickerView
alloc]initWithFrame:CGRectMake(0, 50, self.view.bounds.size.width,
200)];

7–> 返回该UIPickerView指定列中让入选的列表项。

//    [self.view addSubview:self.pickerView];

self.row =  [self.pickerView
selectedRowInComponent:0];//获取给入选的率先列的行数(这里是为着在叫老二排赋值时用底,因此在此之前要清理代理方的推行各个,以免导致不必要之bug)

//    

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

//    //指定数据源和嘱托

四、UIPickerView的UIPickerViewDataSource

//    self.pickerView.delegate = self;

须兑现的不二法门

//    self.pickerView.dataSource = self;

1–>返回显示的列数

//}

– (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView

//#pragma mark 加载数据

{

//-(void)loadData

    return1;// 返回1标志该控件只包含1列

//{

}

//    //需要出示的数目因数组的花样保留

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

//    self.letter = @[@”aaa”,@”bbb”,@”ccc”,@”ddd”];

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

//    self.number = @[@”111″,@”222″,@”333″,@”444″];

//指定每个表盘上发生几实践数据

//}

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

//

{

//#pragma mark UIPickerView DataSource Method 数据源方法

    NSInteger result = 0;

//

    switch (component) {

////指定pickerview有几乎独表盘

        case 0:

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

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

//{

            break;

//    return 2;//第一个显示字母、第二单亮数字

        case 1:

//}

            result = self.number.count;

//

            break;

////指定每个表盘上发出几乎执数据

        default:

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

            break;

//{

    }

//    NSInteger result = 0;

    return result;

//    switch (component) {

}

//        case 0:

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

//            result =
self.letter.count;//根据数组的要素个数返回几实施数据

五、UIPickerView的UIPickerViewDelegate

//            break;

可选方法

//        case 1:

1–>返回每列的幅度

//            result = self.number.count;

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

//            break;

 if (component ==0) {

//            

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

//        default:

}else{

//            break;

 return 100;//第二组键(列)的涨幅

//    }

    }

//    

}

//    return result;

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

//}

2–>返回每列的冲天

//

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

//#pragma mark UIPickerView Delegate Method 代理方

 if (component ==0) {

//

 return 100;//第一组键(列)的莫大

////指定每行如何展示数据(此处和tableview类似)

}else{

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

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

//{

    }

//    NSString * title = nil;

}

//    switch (component) {

– (CGFloat)pickerView:(UIPickerView *)pickerView
rowHeightForComponent:(NSInteger)component
__TVOS_PROHIBITED;//该办法返回的CGFloat值将当该UIPickerView控件中指定列被列表项的冲天。

//        case 0:

3–>设置有同实行显示的题目

//            title = self.letter[row];

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

//            break;

 if (component ==0) {

//        case 1:

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

//            title = self.number[row];

}else{

//            break;

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

//        default:

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

//            break;

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

//    }

return city;

//    

    }

//    return title;

}

//}

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

#pragma mark 两独联动

4–>通过属性字符串设置有一样执行显示的题

– (void)viewDidLoad

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

{

 NSString *str = [_nameArray objectAtIndex:row];

    [super viewDidLoad];

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

    

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

    //加载数据

 return AttributedString;

    [self loadData];

}

    

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

    //指定委托

<注意>如果上述两只艺术还实现了,优先实现attributedTitleForRow的法门

    self.pickerView.delegate = self;

5–>设置有一样推行显示的view视图

    self.pickerView.dataSource = self;

//可以用UILabel 来设置字体大小 和背景颜色 是一个足以由定义设置的艺术

}

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

 

 UILabel *label;//声明一个UILabel

//加载数据

 if (component == 0) {//当是率先推行

-(void)loadData

 //设置frame

{

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

    NSString * path = [[NSBundle mainBundle]pathForResource:@”area”
ofType:@”plist”];

 //将省名赋给label.text

    self.provinces = [NSArray arrayWithContentsOfFile:path];

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

    self.cities = [NSArray
arrayWithArray:self.provinces[0][@”Cities”]];

 //改变字体的颜料

    

label.textColor = [UIColor yellowColor];

    //label的布局约束

 //设置字体大小

    self.label = [[UILabel alloc]initWithFrame:CGRectZero];

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

    self.label.translatesAutoresizingMaskIntoConstraints = NO;

 //改变背景的颜色

    self.label.backgroundColor = [UIColor
groupTableViewBackgroundColor];

        label.backgroundColor = [UIColor redColor];

    self.label.textColor = [UIColor greenColor];

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

    self.label.font = [UIFont systemFontOfSize:30];

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

    [self.label setTextAlignment:NSTextAlignmentCenter];

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

    [self.view addSubview:self.label];

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

    NSArray * labelTop = [NSLayoutConstraint
constraintsWithVisualFormat:@”V:[_pickerView]-30-[_label(50)]”
options:0 metrics:nil
views:NSDictionaryOfVariableBindings(_pickerView,_label)];

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

    NSArray * labelH = [NSLayoutConstraint
constraintsWithVisualFormat:@”H:|-20-[_label]-20-|” options:0
metrics:nil views:NSDictionaryOfVariableBindings(_label)];

label.textColor = [UIColor purpleColor];

    [self.view addConstraints:labelTop];

label.font = [UIFont systemFontOfSize:20];

    [self.view addConstraints:labelH];

        label.backgroundColor = [UIColor redColor];

}

    }

 

 return label;

//有几乎单表盘(component)

}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

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

{

6–>选中某平推行时实行的回调

    return 2;

//被选中的第几排列 的 第几实践

}

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

 

 if(component ==0){

//没个表盘有几执数据(rows)

 //刷新对应列

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

        [self.pickerView reloadComponent:1];

{

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

    NSInteger rows = 0;

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

    switch (component) {

    }

        case 0:

}

            rows = self.provinces.count;//根据plist文件中之数据返回rows

– (void)pickerView:(UIPickerView *)pickerView
didSelectRow:(NSInteger)row inComponent:(NSInteger)component
__TVOS_PROHIBITED;//当用户单击选中该UIPickerView控件的指定列的指定列表项时将见面激发该措施

            break;

六、UIPickerView的拓展

        case 1:

实质上采用中UIPickerView经常与UITextField配合下,给大家提供一个生有利的封装类YLSOPickerView。

            rows = self.cities.count;

调用方法 

            break;

1、导入#import “YLSOPickerView.h”

        default:

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

            break;

#pragma mark UITextFieldDelegate

    }

– (BOOL)textFieldShouldBeginEditing:(UITextField *)textField

    return rows;

{

}

    if (textField.tag==401) {

 

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

//每行的题

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

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

        picker.title = bank;

{

        [picker show];

    NSString * title = nil;

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

    switch (component) {

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

        case 0:

        tapGestureRecognizer.cancelsTouchesInView = YES;

            title = self.provinces[row][@”State”];

        //将触摸事件添加至当下view

            break;

        [picker addGestureRecognizer:tapGestureRecognizer];

        case 1:

        return NO;

            title = self.cities[row][@”city”];

    }

            break;

    return YES;

        default:

}

            break;

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

    }

– (void)keyboardHide:(UITapGestureRecognizer *)tap

    return title;

{

}

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

 

    [pickView quit];

//选中时回调的托方,在这个措施被贯彻省与市之中的联动

}

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

4、-viewDidload里面声明通知

{

– (void)viewDidLoad

    switch (component) {

{

        case
0://选中省份表盘时,根据row的价改变都会往往组,刷新都屡屡组,实现联动

  [super viewDidLoad];

            self.cities = self.provinces[row][@”Cities”];

  //接受通知

            [self.pickerView reloadComponent:1];

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

            break;

}

        case 1:

5、实现通方法

            self.label.text = [NSString
stringWithFormat:@”%@%@”,self.provinces[[self.pickerView
selectedRowInComponent:0]][@”State”],self.cities[[self.pickerView
selectedRowInComponent:1]][@”city”]];//如果当选第二单

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

            break;

{

            

self.textFeild.text = notification.object;

        default:

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

            break;

}

    }

参考:

}

iOS系统UIPickerView的概括用以及方式属性介绍

 

UIPickerView

//修改pickerview的title样式

iOS学习 – UIPickerView

//#pragma mark pickerView Method

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

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

YLSPicker

//{

//    return 1;//表盘数量

//}

//

////判断是孰pickerview,返回相应的rows

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

//{

//    NSInteger rows = 0;

//    

//    if (pickerView == self.pickerView)

//    {

//        rows = self.pickerViewArr.count;

//    }

//    else

//    {

//        rows = self.pickerViewArr2.count;

//    }

//    

//    return rows;

//}

//

////判断是哪个pickerview,返回相应的title

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

//{

//    NSString *str = nil;

//    

//    if (pickerView == self.pickerView)

//    {

//        str = self.pickerViewArr[row];

//    }

//    else

//    {

//        str = self.pickerViewArr2[row];

//    }

//    return str;

//}

//

//#pragma mark 给pickerview设置字体大小和颜色相当

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

//{

//    //可以经从定义label达到从定义pickerview展示数据的道

//    UILabel* pickerLabel = (UILabel*)view;

//    

//    if (!pickerLabel)

//    {

//        pickerLabel = [[UILabel alloc] init];

//        pickerLabel.adjustsFontSizeToFitWidth = YES;

//        pickerLabel.textAlignment = NSTextAlignmentCenter;

//        [pickerLabel setBackgroundColor:[UIColor lightGrayColor]];

//        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];

//    }

//    

//    pickerLabel.text=[self pickerView:pickerView titleForRow:row
forComponent:component];//调用上一个信托方,获得要显示的title

//    return pickerLabel;

//}

////选中某行后回调的方式,获得选中结果

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

//{

//    if (pickerView == self.pickerView)

//    {

//        self.pickerViewSelect = self.pickerViewArr[row];

//        NSLog(@”selected == %@”,self.pickerViewSelect);

//    }

//    else

//    {

//        self.pickerViewSelect2 = self.pickerViewArr2[row];

//    }

//}

 

 

//转自链接:http://www.jianshu.com/p/811882ba8d78

 

 

– (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

 

 

@end