/*********************************三级联动 ********************************/
/** 三级城市选择 */
@property (nonatomic,strong) UIPickerView *cityPickerView;
/** 记录选中的省 */
@property (nonatomic,assign) NSInteger proIndex;
@property (nonatomic,strong) NSMutableArray<ProvinceModel *> *provinceModelArr;
@property (nonatomic,strong) NSMutableArray<areaModel*> *areaModelArr;
设置数据源和代理 UIPickerViewDataSource,UIPickerViewDelegate
#pragma mark - 三级联动
-(void)setUpSelectCityUI{
self.cityPickerView =[[UIPickerViewalloc]init];
self.cityPickerView.backgroundColor =[UIColorwhiteColor];
//显示选中框
self.cityPickerView.showsSelectionIndicator=YES;
self.cityPickerView.dataSource =self;
self.cityPickerView.delegate =self;
self.quyuText.inputView =self.cityPickerView;
}
#pragma mark - pickerView 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
return 3;
}
#pragma mark - pickerView 每列个数
- (NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
// 省会
return self.provinceModelArr.count;
}
if (component == 1) {
// 选中的省会的城市
ProvinceModel *province =self.provinceModelArr[_proIndex];
return province.city.count;
}
else{
// 区域
ProvinceModel *province =self.provinceModelArr[_proIndex];
NSInteger cityIndex = [pickerViewselectedRowInComponent:1];
self.areaModelArr = [areaModelmj_objectArrayWithKeyValuesArray:province.city[cityIndex][@"area"]];
return self.areaModelArr.count;
}
}
#pragma mark - pickerView 每列宽度
- (CGFloat)pickerView:(UIPickerView*)pickerView widthForComponent:(NSInteger)component {
return Kwidth/3;
}
#pragma mark - pickerView 滚动UIPickerView就会调用 返回选中的行
- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
if (component == 0) { // 滚动省会,刷新城市(1列)
// 记录当前选中的省会
_proIndex = [pickerView selectedRowInComponent:0];
[pickerView reloadAllComponents];
}
if (component == 1) {
[pickerView reloadAllComponents];
}
// 获取选中省会
ProvinceModel *province =self.provinceModelArr[_proIndex];
NSLog(@"省-- %@,province_id-- %@ ",province.shortname,province.province_id);
NSInteger cityIndex = [pickerView selectedRowInComponent:1];
NSLog(@" cityIndex %ld ",cityIndex);
NSString *cityName = [province.city[cityIndex]objectForKey:@"shortname"];
NSString *city_id = [province.city[cityIndex]objectForKey:@"city_id"];
NSLog(@"city_id---%@ cityName--- %@ ",city_id,cityName);
NSInteger areaIndex = [pickerView selectedRowInComponent:2];
NSLog(@" areaIndex %ld ",areaIndex);
self.areaModelArr = [areaModelmj_objectArrayWithKeyValuesArray:province.city[cityIndex][@"area"]];
areaModel *area=self.areaModelArr[areaIndex];
NSLog(@"区ID---%@ 区---%@ ",area.area_id, area.shortname);
if([province.shortnameisEqualToString:cityName]){
self.quyuText.text = [NSStringstringWithFormat:@"%@ %@",cityName,area.shortname];
}else{
self.quyuText.text = [NSStringstringWithFormat:@"%@ %@ %@",province.shortname,cityName,area.shortname];
}
}
#pragma mark - pickerView 返回当前行的内容,此处是将数组中数值添加到pivkerView显示栏上
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
// 描述省会
if (component == 0) {
// 获取省会
ProvinceModel *p =self.provinceModelArr[row];
return p.shortname;
}
// 描述市
if (component == 1) {
// 获取选中省会
ProvinceModel *province =self.provinceModelArr[_proIndex];
return [province.city[row]objectForKey:@"shortname"];
}
else{
ProvinceModel *province =self.provinceModelArr[_proIndex];
NSInteger cityIndex = [pickerView selectedRowInComponent:1];
self.areaModelArr = [areaModelmj_objectArrayWithKeyValuesArray:province.city[cityIndex][@"area"]];
areaModel *area=self.areaModelArr[row];
return area.shortname;
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* pickerLabel = (UILabel*)view;
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:12]];
pickerLabel.textAlignment = NSTextAlignmentCenter;
pickerLabel.adjustsFontSizeToFitWidth = YES;
}
// Fill the label text here
pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
return pickerLabel;
}
// 给城市文本框赋值
[selfpickerView:self.cityPickerViewdidSelectRow:0inComponent:0];
---------------------
作者:西半球
来源:CSDN
原文:https://blog.csdn.net/iotjin/article/details/80179809
版权声明:本文为博主原创文章,转载请附上博文链接!