标签:
效果图:
不多说奉上代码:
// // areaPickerView.m // 省市选择器 // // Created by iBokanwisdom on 15/8/31. // Copyright (c) 2015年 pangLee. All rights reserved. // #import "areaPickerView.h" @interface areaPickerView () //创建一个UIPickerView @property (nonatomic,strong) UIPickerView *areaPickerView; //显示当前选定的省市区 @property (nonatomic,strong) UILabel * areaShowLabel; //存储第一次解析的省市字典 @property (nonatomic,strong) NSDictionary * areaData; //存储第二次解析的省市字典 @property (nonatomic,strong) NSDictionary * provinceData; //储存省市数组 @property (nonatomic,strong) NSMutableArray * province; //存放省下级市 @property (nonatomic,strong) NSMutableArray * city; //存放市下级区县 @property (nonatomic,strong) NSMutableArray * district; @end @implementation areaPickerView - (void)viewDidLoad { [super viewDidLoad]; //初始化显示Label self.areaShowLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 270, 320, 50)]; self.areaShowLabel.backgroundColor = [UIColor groupTableViewBackgroundColor]; self.areaShowLabel.textAlignment = NSTextAlignmentCenter; [self.view addSubview: self.areaShowLabel]; //初始化UIPickerViw self.areaPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 55, 320, 160)]; [self.view addSubview:self.areaPickerView]; //先解析plist文件 将数据存储到数组中 [self analysisAreaPlist]; //默认显示第一列第一行第二列第一行 第三列第一行数据 [self analysisAreaPlistGetCityInformationWithComponentRow:0]; [self districtInformationWithComponetRow:0]; //显示Label [self displayLable]; self.areaPickerView.delegate = self; self.areaPickerView.dataSource = self; } - (void)analysisAreaPlist { //第一次解析plist文件 将数据全部存储到areaData字典中 self.areaData = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"area.plist" ofType:nil]]; //初始化省市数组 self.province = [[NSMutableArray alloc]init]; //根据键值 循环获取 省 信息并存到province数组中 for(int i = 0;i < self.areaData.count ;i++) { //从plist文件中可以看出 //从字典中根据i值获取所有键值,获取每个字典下的第一个数据就是省信息 [self.province addObject:[[[self.areaData valueForKey:[NSString stringWithFormat:@"%d",i]] allKeys]objectAtIndex:0]]; } } //根据第一列的省市获取 省市的下级市 - (void)analysisAreaPlistGetCityInformationWithComponentRow:(NSInteger)row { //第二次解析第一次的字典 获取省市下级市的信息存到provinceData中 self.provinceData = [[self.areaData objectForKey:[NSString stringWithFormat:@"%ld",row]] objectForKey:self.province[row]]; //初始化下级市数组 self.city = [[NSMutableArray alloc]init]; //循环获取下级市信息 存到city数组中 for(int i = 0;i < self.provinceData.count;i++) { [self.city addObject:[[[self.provinceData valueForKey:[NSString stringWithFormat:@"%d",i]] allKeys]objectAtIndex:0]]; } } //获取下级市的下级区县信息 - (void)districtInformationWithComponetRow:(NSInteger)row { //初始化区县数组 ??为什么在此初始化而不是在外面? //因为每次滑动改变省市时,省市的下级市,下级区县都是变化的 都是要重新获取的,否则就会造成可变数组数据的继续累积 self.district = [[NSMutableArray alloc]init]; self.district = [[self.provinceData objectForKey:[NSString stringWithFormat:@"%ld",row]] objectForKey:self.city[row]]; } //显示Label - (void)displayLable { //获取第一列当前行 NSString *provinceText =[self.province objectAtIndex:[self.areaPickerView selectedRowInComponent:0]]; //获取第二列当前行 NSString *cityText =[self.city objectAtIndex:[self.areaPickerView selectedRowInComponent:1]]; //获取第三列当前行 NSString *districtText =[self.district objectAtIndex:[self.areaPickerView selectedRowInComponent:2]]; //在label中显示 self.areaShowLabel.text = [NSString stringWithFormat:@"%@-%@-%@",provinceText,cityText,districtText]; } //获取UIPickerView列数 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { //省市 省下级市 区 一共三列 所以返回值为三 return 3; } //获取UIPickerView每列的行数 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if(component == 0) { return self.province.count; } if(component == 1) { return self.city.count; } if(component == 2) { return self.district.count; } return 0; } //设置每列显示什么信息 第一列显示省市 第二列显示下级市 第三列显示下级县区 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { if(component == 0) { return [self.province objectAtIndex:row]; } if(component == 1) { return [self.city objectAtIndex:row]; } if(component == 2) { return [self.district objectAtIndex:row]; } return 0; } //监听滚轮 滚动的是第一列还是第二列 联动事件 - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if(component == 0) { //如果滚动第一列 要根据第一列的行 去获取第二列的信息再根据第二列的行去获取第三列的信息 //默认是滚动第一列显示第二列首行 第三列首行 [self analysisAreaPlistGetCityInformationWithComponentRow:row]; [self districtInformationWithComponetRow:0]; //刷新UIPickerView [self.areaPickerView reloadComponent:1]; [self.areaPickerView reloadComponent:2]; } if(component == 1) { //滚动第二列 第一列不动 第三列随行数变化 [self districtInformationWithComponetRow:row]; [self.areaPickerView reloadComponent:2]; } //显示在Label上 [self displayLable]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
标签:
原文地址:http://www.cnblogs.com/pangrourou/p/4773989.html