
UIPickerView的使用(三)
发布日期:2021-05-09 04:04:13
浏览次数:16
分类:博客文章
本文共 2187 字,大约阅读时间需要 7 分钟。
前两篇文章 、 ,学习了UIPickerView的单列选择器和双列选择器的使用。
现在我们一起学习相互依赖的多列选择器
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 objectForKey:_selectedAreas]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 objectForKey:_selectedAreas]objectAtIndex:row]; }// 当用户选中UIPickerViewDataSource中指定列和列表项时激发该方法- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == 0) { _selectedAreas = [_areas objectAtIndex:row]; [self.pickView reloadComponent:1]; } NSArray *tmp = component == 0 ? _areas: [_teams objectForKey:_selectedAreas]; 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}
效果图:
发表评论
最新留言
逛到本站,mark一下
[***.202.152.39]2025年03月25日 04时39分44秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
中国剩余定理证明过程
2021-05-09
java中Object.equals()简单用法
2021-05-09
poj 2187 Beauty Contest(凸包求解多节点的之间的最大距离)
2021-05-09
java中自动装箱的问题
2021-05-09
zyUpload+struct2完成文件上传
2021-05-09
程序员的开发文档
2021-05-09
mybatis generator修改默认生成的sql模板
2021-05-09
cglib动态代理导致注解丢失问题及如何修改注解允许被继承
2021-05-09
算法 - 如何从股票买卖中,获得最大收益
2021-05-09
大数据开发-Spark-拷问灵魂的5个问题
2021-05-09
算法 - 链表操作思想 && case
2021-05-09
并发编程实战-ConcurrentHashMap源码解析
2021-05-09
C#之反射、元数据详解
2021-05-09
通俗易懂设计模式解析——单例模式
2021-05-09
通俗易懂设计模式解析——抽象工厂模式
2021-05-09
SSM商城项目(十二)
2021-05-09
第5章选择结构程序设计
2021-05-09
前端数据渲染及mustache模板引擎的简单实现
2021-05-09
控制台基于Quartz.Net组件实现定时任务调度(一)
2021-05-09