IOS简单的选择器实现UIPickerView(省+市+区)

轮子的样式:

area.plist的样式:

1.解析文件

1.1解析省

 1 -(void)ProvinceArray
 2 {
 3     
 4     //获取第一层Dictionary
 5     self.allDic = [[NSDictionary alloc]initWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"area" ofType:@"plist"]];
 6     self.provinceArray = [[NSMutableArray alloc]init];
 7     for(int i = 0; i < self.allDic.count; i++)
 8     {
 9         [self.provinceArray addObject:[[[self.allDic objectForKey:[NSString stringWithFormat:@"%d",i]] allKeys] objectAtIndex:0]];
10     }
11 }

1.2解析市

 1 -(void)CityArray:(NSInteger)row
 2 {
 3     //获取第三层Dictionary
 4     self.province = [[self.allDic objectForKey:[NSString stringWithFormat:@"%ld",row]] objectForKey:self.provinceArray[row]];
 5     self.cityArray = [[NSMutableArray alloc]init];
 6     for(int i = 0;i < self.province.count; i++)
 7     {
 8         [self.cityArray addObject:[[[self.province objectForKey:[NSString stringWithFormat:@"%d",i]] allKeys] objectAtIndex:0]];
 9     }
10 }

1.3解析区

1 -(void)CountyArray:(NSInteger)row
2 {
3     self.countyArray = [[NSMutableArray alloc]init];
4     self.countyArray = [[self.province objectForKey:[NSString stringWithFormat:@"%ld",row]] objectForKey:self.cityArray[row]];
5 }

2.绑定并初始化UIPickerView

 1 //以下3个方法实现PickerView的数据初始化
 2 //确定picker的轮子个数
 3 #pragma mark 实现协议UIPickerViewDataSource的方法
 4 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
 5 {
 6     //设置几个滚轮
 7     return 3;
 8 }
 9 //确定picker的每个轮子的item数
10 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
11 {
12     switch (component) {
13         case 0://省份个数
14             return [self.provinceArray count];
15             break;
16         case 1://市的个数
17             return [self.cityArray count];
18             break;
19         case 2://县的个数
20             return [self.countyArray count];
21             break;
22         default:
23             break;
24     }
25     return 0;
26 }
27 //确定每个轮子的每一项显示什么内容
28 #pragma mark 实现协议UIPickerViewDelegate的方法
29 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
30 {
31     switch (component) {
32         case 0://选择省份
33             return [self.provinceArray objectAtIndex:row];
34             break;
35         case 1://选择市
36             return [self.cityArray objectAtIndex:row];
37             break;
38         case 2://选择县
39             return [self.countyArray objectAtIndex:row];
40             break;
41         default:
42             break;
43     }
44     return 0;
45 }

3.监听轮子的滚动

 1 //监听轮子的移动
 2 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
 3 {
 4     if (component == 0) {
 5         [self CityArray:row];
 6         [self CountyArray:0];
 7         [self.pickerView reloadComponent:1];
 8         [self.pickerView reloadComponent:2];
 9     }
10     if (component == 1) {
11         [self CountyArray:row];
12         [self.pickerView reloadComponent:2];
13     }
14 }

 

 

程序代码+area.plist文件 https://files.cnblogs.com/files/guoyongzhi/PickerView.zip

posted @ 2015-08-28 09:34  Locke_Q  阅读(638)  评论(0编辑  收藏  举报