UI进阶--UIPikcerView实现省市联动示例

本文介绍了一种在iOS应用中实现省级与地市级城市选择器的方法。通过自定义UIView和利用UIPickerView来展示和选择中国省份及下属城市的示例。文章提供了完整的代码实现,包括数据模型的创建、UIPickerView的代理方法实现以及响应用户选择事件。
需求:
1.显示2列数据,一列数据为省级城市,另外一列为省级对应下的地市级城市,并可供选择;
2.选择省级城市时,对应的列显示为其省下的地市级城市,并可选择;
3.重新选择省级城市时,对应的地市级城市显示为其省会城市(如地市级城市不在索引为0处);
实现步骤:
1、搭建界面;
2、实现UIPikcerView的代理方法;
3、响应选择事件;
4、完成显示选择数据;
具体代码:
Model:
 1 //
 2 //  JWProvince.h
 3 //  12-27-Province
 4 //
 5 //  Created by xiaomoge on 14/12/27.
 6 //  Copyright (c) 2014年 xiaomoge. All rights reserved.
 7 //
 8 
 9 #import <Foundation/Foundation.h>
10 
11 @interface JWProvince : NSObject
12 @property (nonatomic,copy) NSString *name;
13 @property (nonatomic,strong) NSArray *cities;
14 - (instancetype)initWithDic:(NSDictionary *)dic;
15 + (instancetype)provinceWithDic:(NSDictionary *)dic;
16 + (NSMutableArray *)provinceList;
17 @end

 

 1 //
 2 //  JWProvince.m
 3 //  12-27-Province
 4 //
 5 //  Created by xiaomoge on 14/12/27.
 6 //  Copyright (c) 2014年 xiaomoge. All rights reserved.
 7 //
 8 
 9 #import "JWProvince.h"
10 
11 @implementation JWProvince
12 - (instancetype)initWithDic:(NSDictionary *)dic {
13     if (self = [super init]) {
14         [self setValuesForKeysWithDictionary:dic];
15     }
16     return self;
17 }
18 + (instancetype)provinceWithDic:(NSDictionary *)dic {
19     return [[self alloc] initWithDic:dic];
20 }
21 
22 + (NSMutableArray *)provinceList {
23     NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"provinces" ofType:@"plist"]];
24     
25     NSMutableArray *tempArray = [NSMutableArray array];
26     
27     for (NSDictionary *dic in array) {
28         JWProvince *province = [JWProvince provinceWithDic:dic];
29         [tempArray addObject:province];
30     }
31     return tempArray;
32 }
33 @end

 

Controller:

 1 //
 2 //  ViewController.m
 3 //  12-27-Province
 4 //
 5 //  Created by xiaomoge on 14/12/27.
 6 //  Copyright (c) 2014年 xiaomoge. All rights reserved.
 7 //
 8 
 9 #import "ViewController.h"
10 #import "JWProvince.h"
11 @interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
12 @property (nonatomic,strong) NSArray *provinceDatas;
13 @property (nonatomic,assign) NSInteger index;
14 @end
15 
16 @implementation ViewController
17 #pragma mark - 懒加载
18 - (NSArray *)provinceDatas {
19     if (!_provinceDatas) {
20         _provinceDatas = [JWProvince provinceList];
21     }
22     return _provinceDatas;
23 }
24 #pragma mark - UIPickerView数据源
25 //返回显示数据的列数
26 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
27     return 2;
28 }
29 //返回每列中对应的行的数
30 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
31     if (component == 0) {
32         return self.provinceDatas.count;//第一列显示的个数
33     }
34     JWProvince *pro = self.provinceDatas[self.index];
35     return pro.cities.count;//第二列显示的个数
36 }
37 #pragma mark - UIPickerView代理
38 //返回显示每列以及每行的内容
39 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
40     if (component == 0) {
41         JWProvince *pp = self.provinceDatas[row];
42         return pp.name;//第一列显示的内容
43     }
44     JWProvince *pro = self.provinceDatas[self.index];
45     return pro.cities[row];//第二列显示的内容
46 }
47 #pragma mark - pickerView的选中方法
48 //选中行时做的操作
49 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
50     if (component == 0) {
51         self.index = row;
52         [pickerView reloadComponent:1];//重新加载对应的数据
53         [pickerView reloadAllComponents];//重新加载所有的数据
54         [pickerView selectRow:0 inComponent:1 animated:YES]; //不管之前第二列选中第几行,重新刷新数据后,都显示每二列的第一行
55     }
56 }
57 @end

 

转载于:https://www.cnblogs.com/xiaomoge/p/4200831.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值