标签:
#import "ViewController.h"
@interface ViewController (){
// 储存第二个选取器
NSArray *cityArr;
NSArray *townArr;
}
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic,strong) NSArray *provinces;
@property (nonatomic,strong) NSArray *citys;
@property (nonatomic,strong) NSArray *towns;
@property (nonatomic,strong) NSMutableDictionary *PC;
@property (nonatomic,strong) NSMutableDictionary *CT;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.PC = [NSMutableDictionary dictionary];
self.CT = [NSMutableDictionary dictionary];
self.pickerView.delegate = self;
self.pickerView.dataSource = self;
self.provinces = @[@"山西省",@"江苏省",@"浙江省"];
NSArray *arrSX = @[@"太原市",@"大同市",@"阳泉市",@"长治市",@"晋城市",@"朔州市"];
NSArray *arrJS = @[@"南京市",@"无锡市",@"徐州市",@"常州市",@"苏州市"];
NSArray *arrZJ = @[@"杭州市",@"宁波市",@"温州市",@"嘉兴市",@"湖州市",@"绍兴市",@"金华市",@"衢州市"];
self.citys = @[arrSX, arrJS, arrZJ];
for (int i = 0; i < self.provinces.count ; i++) {
[self.PC addEntriesFromDictionary:@{self.provinces[i]:self.citys[i]}];
}
// 根据第一个滚轮中的值,选取第二个滚轮中的值
NSString *selectedState = [self.provinces objectAtIndex:0];
NSArray *array = [self.PC objectForKey:selectedState];
cityArr = array;
// 山西县城
NSArray *arrTY = @[@"太",@"太",@"太",@"太",@"太",@"原"];
NSArray *arrDT = @[@"大",@"大",@"大",@"大",@"同"];
NSArray *arrYQ = @[@"阳",@"阳",@"阳",@"泉"];
NSArray *arrCZ = @[@"长",@"长",@"长",@"长",@"治"];
NSArray *arrJC = @[@"晋",@"晋",@"晋",@"晋",@"城"];
NSArray *arrSZ = @[@"朔",@"朔",@"朔",@"朔",@"朔",@"朔",@"州"];
NSArray *arrSXT = @[arrTY,arrDT,arrYQ,arrCZ,arrJC,arrSZ];
// 江苏县城
NSArray *arrNJ = @[@"南",@"南",@"南",@"南",@"京"];
NSArray *arrWX = @[@"无",@"无",@"无",@"无",@"无",@"锡"];
NSArray *arrXZ = @[@"徐",@"徐",@"徐",@"徐",@"徐",@"徐",@"徐",@"州"];
NSArray *arrCHZ = @[@"常",@"常",@"常",@"州"];
NSArray *arrSHZ = @[@"苏",@"苏",@"苏",@"苏",@"州"];
NSArray *arrJST = @[arrNJ,arrWX,arrXZ,arrCHZ,arrSHZ];
// 浙江县城
NSArray *arrHZ = @[@"杭",@"杭",@"杭",@"杭",@"州"];
NSArray *arrNB = @[@"宁",@"宁",@"宁",@"波"];
NSArray *arrWZ = @[@"温",@"温",@"温",@"温",@"温",@"州"];
NSArray *arrJX = @[@"嘉",@"嘉",@"嘉",@"嘉",@"嘉",@"嘉",@"兴"];
NSArray *arrHUZ = @[@"湖",@"湖",@"湖",@"湖",@"湖",@"湖",@"湖",@"州"];
NSArray *arrSHX = @[@"绍",@"绍",@"绍",@"绍",@"绍",@"兴"];
NSArray *arrJH = @[@"金",@"金",@"金",@"金",@"华"];
NSArray *arrQZ = @[@"衢",@"衢",@"衢",@"衢",@"州"];
NSArray *arrZJT = @[arrHZ,arrNB,arrWZ,arrJX,arrHUZ,arrSHX,arrJH,arrQZ];
self.towns = @[arrSXT, arrJST, arrZJT];
NSMutableArray *strArr = [NSMutableArray array];
for (NSArray *arr in self.citys) {
for (NSString *str in arr) {
[strArr addObject:str];
}
}
NSMutableArray *townAM = [NSMutableArray array];
for (NSArray *arr in self.towns) {
for (NSArray *arr1 in arr) {
[townAM addObject:arr1];
}
}
for (int i = 0; i < strArr.count ; i++) {
[self.CT addEntriesFromDictionary:@{strArr[i]:townAM[i]}];
}
NSString *selectedState2 = [self.citys[0] objectAtIndex:0];
NSArray *array2 = [self.CT objectForKey:selectedState2];
townArr = array2;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
if (component == 0) {
return self.provinces.count;
}else if (component == 1) {
return [cityArr count];
}
return [townArr count];
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0) {
return [self.provinces objectAtIndex:row];
}else if (component == 1){
return [cityArr objectAtIndex:row];
}
return [townArr objectAtIndex:row] ;
}
-(void)pickerView:(UIPickerView *)pickerViewt didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// 如果选取的是第一个选取器
if (component == 0) {
// 得到第一个选取器的当前行
NSString *selectedState =[self.provinces objectAtIndex:row];
// 根据从pickerDictionary字典中取出的值,选择对应第二个中的值
NSArray *array = [self.PC objectForKey:selectedState];
cityArr = array;
[self.pickerView selectRow:0 inComponent:1 animated:YES];
// 重新装载第二个滚轮中的值
[self.pickerView reloadComponent:1];
}else if (component == 1){
// 得到第一个选取器的当前行
NSString *selectedState1 =[cityArr objectAtIndex:row];
// 根据从pickerDictionary字典中取出的值,选择对应第二个中的值
NSArray *array = [self.CT objectForKey:selectedState1];
townArr = array;
[self.pickerView selectRow:1 inComponent:2 animated:YES];
// 重新装载第二个滚轮中的值
[self.pickerView reloadComponent:2];
}
}
标签:
原文地址:http://www.cnblogs.com/wugenbao/p/5648328.html