您的位置:一分快三全天精准计划 > 1分快3全天计划官方网站-编程 > 运行效果图

运行效果图

2019-09-16 14:13

项目的DEMO可以去我的github上下载网址关于省市区数据是使用json数据,数据借鉴地址:网址,数据比较全,感谢作者的分享;其中直辖市是需要特殊处理的数据,如果处理不好则会出现错误;

图片 1运行效果图

  • 读取json文件的数据
 NSBundle *bundle = [NSBundle mainBundle]; NSString *path = [bundle pathForResource:@"city" ofType:@"json"]; NSData *data = [NSData dataWithContentsOfFile:path]; NSError *error; NSArray *provinceList = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; self.provinceList = provinceList; // 省
  • 将省、市、区分别存储在三个数组中(直辖市的是省和市是相同的)
-getCitydate:(NSInteger)row{ if ([self.provinceList[row][@"type"] intValue] == 0) { NSArray *cityArr = [[NSArray alloc] initWithObjects:self.provinceList[row], nil]; self.cityList = cityArr; }else{ NSMutableArray *cityList = [[NSMutableArray alloc] init]; for (NSArray *cityArr in self.provinceList[row][@"sub"]) { [cityList addObject:cityArr]; } self.cityList = cityList; }}-getAreaDate:(NSInteger)row{ if ([self.provinceList[self.selectOneRow][@"type"] intValue] == 0) { NSMutableArray *areaList = [[NSMutableArray alloc] init]; for (NSArray *cityDict in self.provinceList[self.selectOneRow][@"sub"]) { [areaList addObject:cityDict]; } self.areaList = areaList; }else{ NSMutableArray *areaList = [[NSMutableArray alloc] init]; for (NSArray *cityDict in self.cityList[row][@"sub"]) { [areaList addObject:cityDict]; } self.areaList = areaList; }}
  • 设置UIPickerView的代理方法和数据源
-addPickView{ UIPickerView *pickView = [[UIPickerView alloc] init]; pickView.delegate = self; pickView.dataSource = self; // 设置键盘 self.regionAddress.inputView = pickView;}
  • 实现数据源及代理方法
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 3;}-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component == 0) { return self.provinceList.count; }else if (component == 1){ return self.cityList.count; }else if (component == 2){ return self.areaList.count; } return 0;}-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if (component == 0) { return self.provinceList[row][@"name"]; } if (component == 1){ if ([self.provinceList[self.selectOneRow][@"type"] intValue] == 0) { return self.cityList[0][@"name"]; }else { return self.cityList[row][@"name"]; } } if (component == 2){ return self.areaList[row][@"name"]; } return nil;}
  • 根据选中的数据显示在文本框regionAddress内
-pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ static NSInteger oneRow = 0; static NSInteger tweRow = 0; static NSInteger threeRow = 0; if (component == 0) { self.selectOneRow = row; [self getCitydate:row]; [pickerView reloadComponent:1]; [pickerView selectRow:0 inComponent:1 animated:YES]; [self getAreaDate:0]; [pickerView reloadComponent:2]; [pickerView selectRow:0 inComponent:2 animated:YES]; if ([self.provinceList[self.selectOneRow][@"type"] intValue] == 0) { self.selectTwoRow = 0; } oneRow = row; tweRow = 0; threeRow = 0; } if (component == 1){ self.selectTwoRow = row; [self getAreaDate:row]; [pickerView reloadComponent:2]; [pickerView selectRow:0 inComponent:2 animated:YES]; tweRow = row; threeRow = 0; } if (component == 2){ self.selectThreeRow = row; threeRow = row; } NSMutableString *regionAddress = [[NSMutableString alloc] init]; if (oneRow > 0 &&[self.provinceList[self.selectOneRow][@"type"] intValue] != 0 ) { [regionAddress appendFormat:@"%@省",self.provinceList[self.selectOneRow][@"name"]]; } if (tweRow > 0 || [self.provinceList[self.selectOneRow][@"type"] intValue] == 0){ [regionAddress appendFormat:@"%@市",self.cityList[self.selectTwoRow][@"name"]]; } if (threeRow > 0 ){ [regionAddress appendFormat:@"%@",self.areaList[self.selectThreeRow][@"name"]]; } self.regionAddress.text = regionAddress;}

图片 2运行效果图图片 3运行效果图

本文由一分快三全天精准计划发布于1分快3全天计划官方网站-编程,转载请注明出处:运行效果图

关键词: