項(xiàng)目的DEMO可以去我的github上下載
網(wǎng)址
關(guān)于省市區(qū)數(shù)據(jù)是使用json數(shù)據(jù),數(shù)據(jù)借鑒地址:網(wǎng)址晴及,數(shù)據(jù)比較全颁独,感謝作者的分享奖磁;其中直轄市是需要特殊處理的數(shù)據(jù)渐裂,如果處理不好則會(huì)出現(xiàn)錯(cuò)誤矮锈;
運(yùn)行效果圖
- 讀取json文件的數(shù)據(jù)
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; // 省
- 將省霉翔、市、區(qū)分別存儲(chǔ)在三個(gè)數(shù)組中(直轄市的是省和市是相同的)
-(void)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;
}
}
-(void)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;
}
}
- 設(shè)置UIPickerView的代理方法和數(shù)據(jù)源
-(void)addPickView{
UIPickerView *pickView = [[UIPickerView alloc] init];
pickView.delegate = self;
pickView.dataSource = self;
// 設(shè)置鍵盤(pán)
self.regionAddress.inputView = pickView;
}
- 實(shí)現(xiàn)數(shù)據(jù)源及代理方法
-(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;
}
- 根據(jù)選中的數(shù)據(jù)顯示在文本框regionAddress內(nèi)
-(void)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;
}
運(yùn)行效果圖
運(yùn)行效果圖