服务器之家

服务器之家 > 正文

iOS开发之UIPickerView实现城市选择器的步骤详解

时间:2021-02-02 16:13     来源/作者:iOS开发网

前言

uipickerview是一个选择器控件,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活。uipickerview直接继承了uiview,没有继承uicontrol,因此,它不能像uicontrol那样绑定事件处理方法,uipickerview的事件处理由其委托对象完成。

本文借助于uipickerview来实现城市选择器,第一列为省份,第二列为第一列省份对应的城市或者区,数据放在plist中,plist结构如下图所示,第一层是一个dictionary,每个省份对应的城市是一个array:

iOS开发之UIPickerView实现城市选择器的步骤详解

实现步骤

第一步

拖入一个uipickerviewstoryboard中,然后设置uipickerviewdelegate,和uipickerviewdatasource为当前的控制器,如下图红色区域所示:

iOS开发之UIPickerView实现城市选择器的步骤详解
设置数据源与代理

第二步

在对应的viewcontroller中进行实现,代码注释非常详细

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#import "viewcontroller.h"
 
@interface viewcontroller () <uipickerviewdelegate, uipickerviewdatasource>
 
/**
 * plist对应的字典
 */
@property (nonatomic, strong) nsdictionary* citynames;
/**
 * 省份
 */
@property (nonatomic, strong) nsarray* provinces;
/**
 * 城市
 */
@property (nonatomic, strong) nsarray* cities;
 
@end
 
@implementation viewcontroller
 
/**
 * 懒加载plist
 *
 * @return plist对应的字典
 */
- (nsdictionary*)citynames
{
  if (_citynames == nil) {
 
    nsstring* path = [[nsbundle mainbundle] pathforresource:@"citydata" oftype:@"plist"];
 
    _citynames = [nsdictionary dictionarywithcontentsoffile:path];
  }
 
    return _citynames;
}
 
/**
 * 懒加载省份
 *
 * @return 省份对应的数组
 */
-(nsarray *)provinces
{
  if (_provinces == nil) {
 
    //将省份保存到数组中
    _provinces = [self.citynames allkeys];
 
 
  }
 
  return _provinces;
 
}
 
- (void)viewdidload
{
  [super viewdidload];
  // do any additional setup after loading the view, typically from a nib.
}
 
- (void)didreceivememorywarning
{
  [super didreceivememorywarning];
  // dispose of any resources that can be recreated.
}
 
/**
 * 返回每一列的行数
 *
 * @param pickerview
 * @param component
 *
 * @return
 */
- (nsinteger)pickerview:(uipickerview*)pickerview numberofrowsincomponent:(nsinteger)component
{
 
  if (component == 0) {
    return self.provinces.count;
  }
  else {
 
    [self loaddata:pickerview];
 
    return self.cities.count;
  }
}
 
/**
 * 返回每一行显示的文本
 *
 * @param pickerview
 * @param row
 * @param component
 *
 * @return
 */
- (nsstring*)pickerview:(uipickerview*)pickerview titleforrow:(nsinteger)row forcomponent:(nsinteger)component
{
 
  //第一列返回所有的省份
  if (component == 0) {
    return self.provinces[row];
  }
  else {
 
    [self loaddata:pickerview];
 
    return self.cities[row];
  }
}
 
 
/**
 * 加载第二列显示的数据
 *
 * @param pickerview
 */
-(void)loaddata:(uipickerview*)pickerview
{
 
  //一定要首先获取用户选择的那一行 然后才可以根据选中行获取省份 获取省份以后再去字典中加载省份对应的城市
  nsinteger selrow = [pickerview selectedrowincomponent:0];
 
  nsstring *key = self.provinces[selrow];
 
  self.cities = [self.citynames valueforkey:key];
 
}
 
/**
 * 一共多少咧
 *
 * @param pickerview
 *
 * @return
 */
- (nsinteger)numberofcomponentsinpickerview:(uipickerview*)pickerview
{
  return 2;
}
 
/**
 * 选中某一行后回调 联动的关键
 *
 * @param pickerview
 * @param row    用户选择的省份
 * @param component
 */
- (void)pickerview:(uipickerview*)pickerview didselectrow:(nsinteger)row incomponent:(nsinteger)component
{
 
  if (component == 0) {
 
    //重新加载第二列的数据
    [pickerview reloadcomponent:1];
    //让第二列归位
    [pickerview selectrow:0 incomponent:1 animated:yes];
  }
}
 
@end

实现效果

iOS开发之UIPickerView实现城市选择器的步骤详解

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部