/*********************************三級(jí)聯(lián)動(dòng) ********************************/
/** 三級(jí)城市選擇 */
@property (nonatomic,strong) UIPickerView *cityPickerView;
/** 記錄選中的省 */
@property (nonatomic,assign) NSInteger proIndex;
@property (nonatomic,strong) NSMutableArray<ProvinceModel *> *provinceModelArr;
@property (nonatomic,strong) NSMutableArray<areaModel*> *areaModelArr;
設(shè)置數(shù)據(jù)源和代理?UIPickerViewDataSource,UIPickerViewDelegate
#pragma mark - 三級(jí)聯(lián)動(dòng)
-(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 列數(shù)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView*)pickerView {
? ? return 3;
}
#pragma mark - pickerView 每列個(gè)數(shù)
- (NSInteger)pickerView:(UIPickerView*)pickerView numberOfRowsInComponent:(NSInteger)component {
? ? ? ? ? ? if (component == 0) {
? ? ? ? ? ? ? ? // 省會(huì)
? ? ? ? ? ? ? ? return self.provinceModelArr.count;
? ? ? ? ? ? }
? ? ? ? ? ? if (component == 1) {
? ? ? ? ? ? ? ? // 選中的省會(huì)的城市
? ? ? ? ? ? ? ? ProvinceModel *province =self.provinceModelArr[_proIndex];
? ? ? ? ? ? ? ? return province.city.count;
? ? ? ? ? ? }
? ? ? ? ? ? else{
? ? ? ? ? ? ? ? // 區(qū)域
? ? ? ? ? ? ? ? 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 滾動(dòng)UIPickerView就會(huì)調(diào)用? 返回選中的行
- (void)pickerView:(UIPickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
? ? if (component == 0) { // 滾動(dòng)省會(huì),刷新城市(1列)
? ? ? ? // 記錄當(dāng)前選中的省會(huì)
? ? ? ? _proIndex = [pickerView selectedRowInComponent:0];
? ? ? ? [pickerView reloadAllComponents];
? ? }
? ? if (component == 1) {
?? ? ? [pickerView reloadAllComponents];
? ? }
?? ? ? // 獲取選中省會(huì)
? ? ? ? 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(@"區(qū)ID---%@? 區(qū)---%@ ",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 返回當(dāng)前行的內(nèi)容,此處是將數(shù)組中數(shù)值添加到pivkerView顯示欄上
-(NSString*)pickerView:(UIPickerView*)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
? ? ? ? // 描述省會(huì)
? ? ? ? if (component == 0) {
? ? ? ? ? ? // 獲取省會(huì)
? ? ? ? ? ? ProvinceModel *p =self.provinceModelArr[row];
? ? ? ? ? ? return p.shortname;
? ? ? ? }
? ? // 描述市
? ? if (component == 1) {
? ? ? ? // 獲取選中省會(huì)
? ? ? ? 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];
---------------------
作者:西半球
來(lái)源:CSDN
原文:https://blog.csdn.net/iotjin/article/details/80179809
版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!