*********
#import "ViewController.h" #import "Province.h" @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate> @property (nonatomic,strong)NSArray *provinces; @property (nonatomic,assign)NSInteger indexOfProvice;//当前默认选中的省份 @end @implementation ViewController -(NSArray *)provinces{ if (!_provinces) { _provinces = [Province provinceList]; } return _provinces; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog(@"%@",self.provinces); } // returns the number of ‘columns‘ to display. - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2; } // returns the # of rows in each component.. - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component == 0) {//省分 return self.provinces.count; } //获取对应省份的城市个数 Province *province = self.provinces[self.indexOfProvice]; return province.cities.count; } #pragma mark 显示数据 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if(component == 0){//显示省份的名字 //对应列行的省份 Province *province = self.provinces[row]; return province.name; } //获取选中的城市,显示城市名字 Province *selectedProvice = self.provinces[self.indexOfProvice]; return selectedProvice.cities[row]; } //-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ // // UILabel *label = (UILabel *)view; // if (!label) { // label = [[UILabel alloc] init]; // } // // if(component == 0){//显示省份的名字 // //对应列行的省份 // Province *province = self.provinces[row]; // label.text = province.name; // label.backgroundColor = [UIColor grayColor]; // }else{ // // //获取选中的城市,显示城市名字 // Province *selectedProvice = self.provinces[self.indexOfProvice]; // label.text = selectedProvice.cities[row]; // label.backgroundColor = [UIColor blueColor]; // } // // // // // return label; //} #pragma mark 选中行 -(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == 0) {//第一列省的选中改变后,就要更新第二列数据 //更新选中省份的索引 self.indexOfProvice = row; //刷新数据 //全部刷新 //[pickerView reloadAllComponents]; //部份刷新 [pickerView reloadComponent:1]; //不管之前第二列选中第几行,重新刷新数据后,都显示每二列的第一行 [pickerView selectRow:0 inComponent:1 animated:YES]; } } #pragma mark 设置宽度 -(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (component == 0) { return 80; } return 200; } @end
***model.m
#import "Province.h" @implementation Province -(instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self; } +(instancetype)provinceWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict]; } +(NSArray *)provinceList{ //plist文件路径 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil]; NSArray *provincePlist = [NSArray arrayWithContentsOfFile:filePath]; NSMutableArray *provinceM = [NSMutableArray array]; for (NSDictionary *dic in provincePlist) { Province *prov = [Province provinceWithDict:dic]; [provinceM addObject:prov]; } return provinceM; } @end
****model.h
#import <UIKit/UIKit.h> @interface Province : NSObject @property(nonatomic,copy)NSString *name; @property(nonatomic,strong)NSArray *cities; -(instancetype)initWithDict:(NSDictionary *)dict; +(instancetype)provinceWithDict:(NSDictionary *)dict; +(NSArray *)provinceList; @end
时间: 2024-10-17 12:50:21