UIPickerView选择省市
来源:互联网 发布:ios wkwebview 传值js 编辑:程序博客网 时间:2024/04/30 11:53
1.遵守协议<UIPickerViewDataSource, UIPickerViewDelegate>//存放省份@property (nonatomic, strong) NSMutableArray *shengFen;//存放城市@property (nonatomic, strong) NSArray *city;//存放 省份->城市 字典@property (nonatomic, strong) NSMutableDictionary *dict;//全局pickerView@property (nonatomic, weak) UIPickerView *pickerView;@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; [self loadData]; [self _initPickerView]; /**初始数据*/ //显示第一个省份 NSString *shengFen = [_shengFen objectAtIndex:index]; //用省份做Key取出城区数组,这样刚初始化完后就会显示省份对应的城区了 _city = [_dict objectForKey:shengFen];}//创建pickerView- (void)_initPickerView{ /**创建pickerView*/ UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(20, 40, 300, 300)]; [self.view addSubview:pickerView]; self.pickerView = pickerView; /**设置代理*/ pickerView.dataSource = self; pickerView.delegate = self; }//返回多少列- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2;}//每列多少行- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component == 0) { return _shengFen.count; }else{ return _city.count; }}//设置标题- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component == 0) { return _shengFen[row]; }else{ return _city[row]; } return 0;}//点击单元格调用- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == 0) { /**取出当前选择的省份*/ NSString *seleteShengFen = [_shengFen objectAtIndex:row]; /**用省份做key取出数组赋值给city*/ _city = [_dict objectForKey:seleteShengFen]; /**刷新数据*/ [pickerView reloadComponent:1]; } /**取得第0列和第1列当前显示的行数 从数组取得对应数据*/ NSInteger index = [pickerView selectedRowInComponent:0]; NSInteger index1 = [pickerView selectedRowInComponent:1]; self.lable.text = [NSString stringWithFormat:@"%@:%@",_shengFen[index] , _city[index1]]; }//加载数据- (void)loadData{ NSString *path = [[NSBundle mainBundle] pathForResource:@"city.plist" ofType:nil]; NSArray *arr = [NSArray arrayWithContentsOfFile:path]; _shengFen = [[NSMutableArray alloc] init]; _dict = [NSMutableDictionary dictionary]; for (NSDictionary *dict in arr) { //城市列表 NSArray *city = dict[@"cities"]; //取出省份 存入数组 NSString *str = [dict objectForKey:@"state"]; [_shengFen addObject:str]; //取出城市存入数组 _city = [NSArray arrayWithArray:city]; [_dict setObject:city forKey:str]; } }
0 0
- UIPickerView选择省市
- UIPickerView【随机点菜系统 && 省市联动 && 选择国旗】
- Swift - 省市县三级联动功能的实现(使用UIPickerView选择框)
- 省市选择
- UIPickerView(滚动选择控制器)
- iOS uipickerview 选择DATE
- UIPickerView + 城市选择
- UIPickerView城市选择
- [IOS]UIPickerView选择时间段
- UIPickerView - 城市选择
- UIPickerView(选择框)
- 创建关联的UIPickerView 显示省市
- 40-使用UIPickerView自定义省市键盘
- UIPickerView做好的时间选择
- 中国省市选择列表
- 省市选择_js封装
- 省市选择源码
- js简单省市选择
- hdu 1242 Rescue(BFS搜索)
- node及npm安装过程中的win环境变量设置
- android_checkbox_dialog 设计 是不是要开起 默认不提示对话框
- POI操作Excel详解,HSSF和XSSF两种方式
- 索引
- UIPickerView选择省市
- Android仿虾米音乐播放器之专辑图片模糊处理
- 【SSO】--单点登录之过滤器(filter)
- Android ViewDragHelper完全解析 自定义ViewGroup神器
- STM32F4 按键FIFO设计
- USACO Prime Palindromes(dfs)
- MySql基本查询、连接查询、子查询、正则表达查询讲解
- 递归调用
- 并查集