iOS学习笔记iOS高阶UI相关iOS Developer

UIPickerView--省市区三级联动

2016-04-05  本文已影响1948人  芝麻绿豆

项目的DEMO可以去我的github上下载
网址
关于省市区数据是使用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; // 省
-(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;
    }
}
-(void)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;
}
-(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;
}
运行效果图 运行效果图
上一篇下一篇

猜你喜欢

热点阅读