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

項(xiàng)目的DEMO可以去我的github上下載
網(wǎng)址
關(guān)于省市區(qū)數(shù)據(jù)是使用json數(shù)據(jù),數(shù)據(jù)借鑒地址:網(wǎng)址晴及,數(shù)據(jù)比較全颁独,感謝作者的分享奖磁;其中直轄市是需要特殊處理的數(shù)據(jù)渐裂,如果處理不好則會(huì)出現(xiàn)錯(cuò)誤矮锈;

運(yùn)行效果圖

  • 讀取json文件的數(shù)據(jù)
    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; // 省
  • 將省霉翔、市、區(qū)分別存儲(chǔ)在三個(gè)數(shù)組中(直轄市的是省和市是相同的)
-(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;
    }
}
  • 設(shè)置UIPickerView的代理方法和數(shù)據(jù)源
-(void)addPickView{
    UIPickerView *pickView = [[UIPickerView alloc] init];
    pickView.delegate = self;
    pickView.dataSource = self;
    // 設(shè)置鍵盤(pán)
    self.regionAddress.inputView = pickView;
}
  • 實(shí)現(xiàn)數(shù)據(jù)源及代理方法
-(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;
}
  • 根據(jù)選中的數(shù)據(jù)顯示在文本框regionAddress內(nèi)
-(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;
}
運(yùn)行效果圖
運(yùn)行效果圖
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末苞笨,一起剝皮案震驚了整個(gè)濱河市债朵,隨后出現(xiàn)的幾起案子子眶,更是在濱河造成了極大的恐慌,老刑警劉巖序芦,帶你破解...
    沈念sama閱讀 218,204評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件臭杰,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡谚中,警方通過(guò)查閱死者的電腦和手機(jī)渴杆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,091評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)宪塔,“玉大人磁奖,你說(shuō)我怎么就攤上這事∧晨穑” “怎么了点寥?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,548評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)来吩。 經(jīng)常有香客問(wèn)我,道長(zhǎng)蔽莱,這世上最難降的妖魔是什么弟疆? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,657評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮盗冷,結(jié)果婚禮上怠苔,老公的妹妹穿的比我還像新娘。我一直安慰自己仪糖,他們只是感情好柑司,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,689評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著锅劝,像睡著了一般攒驰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上故爵,一...
    開(kāi)封第一講書(shū)人閱讀 51,554評(píng)論 1 305
  • 那天玻粪,我揣著相機(jī)與錄音,去河邊找鬼诬垂。 笑死劲室,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的结窘。 我是一名探鬼主播很洋,決...
    沈念sama閱讀 40,302評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼隧枫!你這毒婦竟也來(lái)了喉磁?” 一聲冷哼從身側(cè)響起谓苟,我...
    開(kāi)封第一講書(shū)人閱讀 39,216評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎线定,沒(méi)想到半個(gè)月后娜谊,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,661評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡斤讥,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,851評(píng)論 3 336
  • 正文 我和宋清朗相戀三年纱皆,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片芭商。...
    茶點(diǎn)故事閱讀 39,977評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡派草,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出铛楣,到底是詐尸還是另有隱情近迁,我是刑警寧澤,帶...
    沈念sama閱讀 35,697評(píng)論 5 347
  • 正文 年R本政府宣布簸州,位于F島的核電站鉴竭,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏岸浑。R本人自食惡果不足惜搏存,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,306評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望矢洲。 院中可真熱鬧璧眠,春花似錦、人聲如沸读虏。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,898評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)盖桥。三九已至灾螃,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間葱轩,已是汗流浹背睦焕。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,019評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留靴拱,地道東北人垃喊。 一個(gè)月前我還...
    沈念sama閱讀 48,138評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像袜炕,于是被迫代替她去往敵國(guó)和親本谜。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,927評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容