iOS中UIPickerView实现省/市连动
UIPickerView实现省/市的连动
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource> 4 @property(strong,nonatomic) UIPickerView *myPickView; 5 @property(strong,nonatomic) NSArray *array;//用于存储数据 6 @property(strong,nonatomic) NSMutableArray *provinceArray;//省份数组 7 @property(strong,nonatomic) NSMutableArray *cityArray;//城市数组 8 @property(strong,nonatomic) UIAlertController *alterController; 9 @property(strong,nonatomic) UIAlertAction *actionA; 10 @property(strong,nonatomic) UIAlertAction *actionB; 11 12 @end
1 #import "ViewController.h" 2 3 @interface ViewController () 4 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 //初始化 13 self.provinceArray=[NSMutableArray array]; 14 self.cityArray=[NSMutableArray array]; 15 16 self.myPickView=[[UIPickerView alloc] initWithFrame:CGRectMake(30, 100, 350, 200)]; 17 self.myPickView.backgroundColor=[UIColor greenColor]; 18 //指定代理 19 self.myPickView.delegate=self; 20 self.myPickView.dataSource=self; 21 22 [self.view addSubview:self.myPickView]; 23 24 //提取文件 25 NSString *path=[[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"]; 26 self.array=[NSArray arrayWithContentsOfFile:path]; 27 28 //把地区数据提取出来存储到 dic 字典中 29 for (NSDictionary *dic in self.array) { 30 //把字典 dic 中的省提取出来放到集合 provinceArray 中 31 [self.provinceArray addObject:dic[@"State"]]; 32 } 33 //NSLog(@"%@",self.provinceArray); 34 35 36 37 38 }
1 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 2 { 3 return 2; //输出的是两列 4 } 5 6 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 7 { 8 if (component==0) { 9 return self.provinceArray.count; 10 }else 11 { 12 return self.cityArray.count; 13 } 14 } 15 16 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 17 { 18 if (component==0) { 19 return [self.provinceArray objectAtIndex:row]; 20 } 21 return [self.cityArray objectAtIndex:row]; 22 } 23 24 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component 25 { 26 if (component==0) 27 { 28 //清除上次选择的城市 29 [self.cityArray removeAllObjects]; 30 NSDictionary *dic=[self.array objectAtIndex:row]; 31 NSArray *array1=dic[@"Cities"]; 32 NSMutableArray *cityA=[NSMutableArray array]; 33 for (NSDictionary *tempdic in array1) { 34 [cityA addObject:tempdic[@"city"]]; 35 } 36 self.cityArray=cityA; 37 [self.myPickView selectRow:row inComponent:0 animated:YES]; 38 //刷新组件 39 [self.myPickView reloadComponent:1]; 40 } 41 if(component==1){ 42 43 NSInteger integerFirst=[self.myPickView selectedRowInComponent:0]; 44 NSInteger integerTow=[self.myPickView selectedRowInComponent:1]; 45 NSString *firstStr=[self.provinceArray objectAtIndex:integerFirst]; 46 NSString *towStr=[self.cityArray objectAtIndex:integerTow]; 47 NSString *str=[NSString stringWithFormat:@"您要选择的是:%@, %@吗?",firstStr,towStr]; 48 self.alterController=[UIAlertController alertControllerWithTitle:@"系统提示" message:str preferredStyle:UIAlertControllerStyleAlert]; 49 self.actionA=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; 50 self.actionB=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]; 51 [self.alterController addAction:self.actionA]; 52 [self.alterController addAction:self.actionB]; 53 [self presentViewController:self.alterController animated:YES completion:nil]; 54 } 55 56 } 57 58 -(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component 59 { 60 return 40; 61 } 62 - (void)didReceiveMemoryWarning { 63 [super didReceiveMemoryWarning]; 64 // Dispose of any resources that can be recreated. 65 }