博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UIPickerView的使用(二)
阅读量:6966 次
发布时间:2019-06-27

本文共 1950 字,大约阅读时间需要 6 分钟。

 上篇文章 学习了如何创建单列选择器,现在看一下如何创建多列选择器

 多列选择器(以二列为例)

 1、遵守协议和创建两个数据源

2、创建pickView

 

3、实现代理

 

//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件包含的列数- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView{    return 2; // 返回2表明该控件只包含2列}//UIPickerViewDataSource中定义的方法,该方法的返回值决定该控件指定列包含多少个列表项- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{    // 如果该控件只包含一列,因此无须理会列序号参数component    // 该方法返回teams.count,表明teams包含多少个元素,该控件就包含多少行    if (component == 0) {        return _areas.count;    }    else        return _teams.count;    }// UIPickerViewDelegate中定义的方法,该方法返回的NSString将作为UIPickerView中指定列和列表项的标题文本- (NSString *)pickerView:(UIPickerView *)pickerView             titleForRow:(NSInteger)row forComponent:(NSInteger)component{    // 由于该控件只包含一列,因此无须理会列序号参数component    // 该方法根据row参数返回teams中的元素,row参数代表列表项的编号,    // 因此该方法表示第几个列表项,就使用teams中的第几个元素    if (component == 0) {        return [_areas objectAtIndex:row];    }    return [_teams objectAtIndex:row];}// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{        NSArray *tmp = component == 0 ? _areas: _teams;    NSString *tip = component == 0 ? @"区域":@"球队";    // 使用一个UIAlertView来显示用户选中的列表项    UIAlertView* alert = [[UIAlertView alloc]                          initWithTitle:@"提示"                          message:[NSString stringWithFormat:@"你选中的%@是:%@"                                   , tip ,[ tmp objectAtIndex:row]]                          delegate:nil                          cancelButtonTitle:@"确定"                          otherButtonTitles:nil];    [alert show];}// UIPickerViewDelegate中定义的方法,该方法返回的NSString将作为// UIPickerView中指定列的宽度-(CGFloat)pickerView:(UIPickerView *)pickerView   widthForComponent:(NSInteger)component{    // 如果是第一列,宽度为90    if(component == 0) {        return 90;    }    return 210; // 如果是其他列(只有第二列),宽度为210}

 

效果图

 

下一篇   是相关联的多列选择器的使用。

转载地址:http://ojwsl.baihongyu.com/

你可能感兴趣的文章
rhsl 6.5 搭建DNS服务器
查看>>
推荐最适合IT人自学的6个视频网站、8个社区网站,欢迎补充
查看>>
idea配置tomcat
查看>>
自定义Iptables日志输出
查看>>
Http Cite
查看>>
grep精确匹配(存在. -的情况)
查看>>
鼠标滑在标题上显示图片的JS代码
查看>>
View Horizon Mirage安装手册(一)——Horizon Mirage介绍
查看>>
Mac OSX 正确地同时安装Python 2.7 和Python3
查看>>
python 爬虫之BeautifulSoup 库的基本使用
查看>>
Linux 文件查找命令详解
查看>>
MySQL中权限管理
查看>>
JavaEE 请求响应
查看>>
如何解决数据文件传输的风险?
查看>>
Hyper-v集群高可用性配置
查看>>
查看域名解析是否生效的方法
查看>>
javax.crypto.spec.SecretKeySpec
查看>>
ubuntu14.04搭建Nginx+php+mysql+phpmyadmin
查看>>
fatal error C1083: 无法打开预编译头文件:“Debug\a.pch”: No su
查看>>
Linux Crontab定时任务
查看>>