IOS pickerView 使用
源码下载
选择器,如同html select 一个pickerView可以实现多个select 关键是由numberOfComponentsInPickerView 这个决定有多少个列
实现委托和数据源。
实现下面三个方法
// 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 [array1 count];
}else{;
return [array2 count];
}
}
//设置当前内容
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component==0){
return [array1 objectAtIndex:row];
}else{
return [array2 objectAtIndex:row];
}
}
实现效果:
获取选择到的数据
联动效果
需要定义一个字典类型。相当于java Map类型
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"DDDDD");
mainArray = [NSArray arrayWithObjects:@"test1",@"test2",nil];
myds = [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:@"test11",@"test12", nil],@"test1",
[NSArray arrayWithObjects:@"test21",@"test22", nil],@"test2", nil];
subArray = [myds objectForKey:@"test1"];
_pick4.delegate=self;
_pick4.dataSource=self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
// 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 [mainArray count];
}else{;
return [subArray count];
}
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if(component==0){
return [mainArray objectAtIndex:row];
}else{
return [subArray objectAtIndex:row];
}
}
//选择相应列表的时候
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
if(component==0){
subArray = [myds objectForKey:[mainArray objectAtIndex:row]];
[pickerView selectRow:0 inComponent:1 animated:YES];
[pickerView reloadComponent:1 ];
}
}
//实现读取内容
- (IBAction)button4click:(id)sender {
_text4.text = @"DDDD";
NSInteger ar1 = [_pick4 selectedRowInComponent:0];
NSInteger ar2 = [_pick4 selectedRowInComponent:1];
NSString *str1 = [mainArray objectAtIndex:ar1];
NSString *str2 = [subArray objectAtIndex:ar2];
NSString *message = [ str1 stringByAppendingFormat:@"---%@",str2 ];
_text4.text = message;
}
IOS pickerView 使用相关推荐
- iOS pickerView(所有类型一网打尽)
概述 关于PickView的所有类型都在这里 详细 代码下载:http://www.demodashi.com/demo/11017.html 首先看下项目的整体结构: 一.准备工作 UIPicker ...
- ios pickerview选择城市
// // ViewController.m // 02-城市选中 // // Created by panba on 16-4-12. // Copyright (c) 2016年 panba. A ...
- 安卓开源项目周报1220
由OpenDigg 出品的安卓开源项目周报第二期来啦.我们的安卓开源周报集合了OpenDigg一周来新收录的优质的安卓开发方面的开源项目,方便安卓开发人员便捷的找到自己需要的项目工具等. androi ...
- 最全android Demo
1.BeautifulRefreshLayout-漂亮的美食下拉刷新 https://github.com/android-cjj/BeautifulRefreshLayout/tree/Beauti ...
- 140款Android开源优秀项目源码
140款Android开源优秀项目源码 BeautifulRefreshLayout-漂亮的美食下拉刷新 https://github.com/android-cjj/BeautifulRefresh ...
- GitHub 上受欢迎的 Android UI Library 整理(一)
内容较多,可以用 ctrl+F 来搜索 抽屉菜单 https://github.com/mikepenz/MaterialDrawer ★7337 - 安卓抽屉效果实现方案 https://githu ...
- Github常用框架集合
Script桥接 https://github.com/JackyAndroid/AndroidChromium ★1476 - 谷歌浏览器安卓版源码项目 https://github.com/The ...
- GitHub 上受欢迎的 Android UI Library 整理
抽屉菜单 https://github.com/mikepenz/MaterialDrawer ★7337 - 安卓抽屉效果实现方案 https://github.com/Yalantis/Side- ...
- GitHub 上受欢迎的 Android UI Library(part_one)
抽屉菜单 https://github.com/mikepenz/MaterialDrawer ★7337 - 安卓抽屉效果实现方案 https://github.com/Yalantis/Side- ...
最新文章
- CPU 深夜狂飙,一帮大佬都傻眼了......
- 无法在终端中显示Git树
- python查看环境路径
- Qt与OpenCV编程:在QLabel加载的图像上画矩形并剪切
- external libraries里没有maven包_Maven企业实战系列(三):彻底看懂maven的体系结构...
- 前端常见的安全性问题有哪些?
- 使用友盟+的APM服务实现对移动端APP的性能监控
- 微信小程序开发教程第八章:微信小程序分组开发与左滑功能实现
- 安装注册数据库管理工具
- java匿名类对象的坏处_java匿名内部类的使用注意事项
- 仿百度的输入提示功能
- python+webdriver(二)
- su and sudo
- 清退117名博士、119名硕士!研究生“严出”成人才培养大趋势
- java 集合教程_Java Collections
- 2021 年 35+ 最佳免费 WordPress 博客主题
- IT公司面试 逻辑题目
- 由对称引起的空间结构耦合效应
- Invalid row number (65536) outside allowable range (0..65535)
- LNK1104:无法打开文件\.obj