美文网首页
iOS 省市区三级联动选择器UIPickerView 固定顺序

iOS 省市区三级联动选择器UIPickerView 固定顺序

作者: Jesscia_Liu | 来源:发表于2017-09-14 14:17 被阅读0次

(文末附github下载地址)

效果图

numberRoll.gif

省市区三级联动思路(UIPickerView)

  • 使用plist存储"省、市、区"信息
    • 注意:字典是无序的而数组是有序的,所以为避免取出的省顺序错乱, 此处使用两个plist文件,一个用于存储省的信息(数组存省名字符串), 一个用于存储对应省的市和区的信息(键:省名,值:市的数组-区的数组).
  • 读取省名plist,并存储为数组provinceArray
  • 读取地区plist,并存储为字典pickerDict(以各省名为键)
  • 默认选取第一个省的信息,为当前选中信息并存储为selectedArray
  • 创建pickerView,设置三列,设置数据源,通过行号和键值取得对应联动数据
  • 实现pickerView代理,获得当前选中省市区数据

UIPickerView关键代码实现

#pragma mark - get data
- (void)getPickerData {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];
    NSString *provencePath = [[NSBundle mainBundle] pathForResource:@"Provence" ofType:@"plist"];
    NSArray *provenceArr = [[NSArray alloc] initWithContentsOfFile:provencePath];
    
    self.pickerDict = [[NSDictionary alloc] initWithContentsOfFile:path];
    self.provinceArray = provenceArr;
    self.selectedArray = [self.pickerDict objectForKey:[provenceArr objectAtIndex:0]];
    
    if (self.selectedArray.count > 0) {
        self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
    }
    
    if (self.cityArray.count > 0) {
        self.districtArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
    }
}

#pragma mark - <UIPickerViewDelegate>
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return self.provinceArray.count;
    } else if (component == 1) {
        return self.cityArray.count;
    } else {
        return self.districtArray.count;
    }
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        return [self.provinceArray objectAtIndex:row];
    } else if (component == 1) {
        return [self.cityArray objectAtIndex:row];
    } else {
        return [self.districtArray objectAtIndex:row];
    }
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component == 0) {
        return 110;
    } else if (component == 1) {
        return 100;
    } else {
        return 110;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        self.selectedArray = [self.pickerDict objectForKey:[self.provinceArray objectAtIndex:row]];
        if (self.selectedArray.count > 0) {
            self.cityArray = [[self.selectedArray objectAtIndex:0] allKeys];
        } else {
            self.cityArray = nil;
        }
        if (self.cityArray.count > 0) {
            self.districtArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:0]];
        } else {
            self.districtArray = nil;
        }
    }

    if (component == 0) {
        [pickerView selectRow:0 inComponent:1 animated:NO];
        [pickerView selectRow:0 inComponent:2 animated:NO];
    }
    if (component == 1) {
        if (self.selectedArray.count > 0 && self.cityArray.count > 0) {
            self.districtArray = [[self.selectedArray objectAtIndex:0] objectForKey:[self.cityArray objectAtIndex:row]];
        } else {
            self.districtArray = nil;
        }
        [pickerView selectRow:0 inComponent:2 animated:NO];
    }
    
    [pickerView reloadAllComponents];
}

github下载地址

https://github.com/KrystalNa/LNAddressPickerView

相关文章

网友评论

      本文标题:iOS 省市区三级联动选择器UIPickerView 固定顺序

      本文链接:https://www.haomeiwen.com/subject/gwmlsxtx.html