ios--- UIpickerView 省市區(qū)三級(jí)聯(lián)動(dòng)

/*********************************三級(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)附上博文鏈接!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 學(xué)習(xí)階段五(update:2013-3-28 0:38):實(shí)現(xiàn)功能:為減少工作量,僅抓取所有tag頁(yè)下前3頁(yè)電影信...
    瓦力求索閱讀 363評(píng)論 0 3
  • 正則表達(dá)式是一種文本模式,包括普通字符(例如,a 到 z 之間的字母)和特殊字符(稱(chēng)為“元字符”)。模式描述在搜索...
    Eenmaal_7a01閱讀 272評(píng)論 0 0
  • //swift后面沒(méi)有分號(hào)。。加分號(hào)不會(huì)報(bào)錯(cuò)。。但是規(guī)定不能加分號(hào) //引入框沒(méi)有分號(hào) 直接寫(xiě)就OK //swi...
    光_遙遠(yuǎn)閱讀 645評(píng)論 0 0
  • 爺爺病重去世,爸媽吵架鬧離婚,已實(shí)習(xí)公司遲遲不給錄用回復(fù),此為背景(感覺(jué)被全世界拋棄了) 2017.10.17 第...
    必雨軒閱讀 351評(píng)論 0 0
  • 圣誕節(jié)的時(shí)候,跟多年的男閨蜜聊天說(shuō)還沒(méi)吃飯,然后他大半夜地點(diǎn)了外送黃燜雞,吃完,室友默默地把這本書(shū)塞到我懷里,鄭重...
    清洺閱讀 312評(píng)論 0 0

友情鏈接更多精彩內(nèi)容