iOS國家城市選取器 讀取本地json檔案

來源:互聯網
上載者:User

標籤:ios   pickerview   城市選取器   國家選擇   本地json   

最近在做的產品有這麼個需求,讀取本地json檔案中的國家和城市資訊,顯示到pickerview上,在網上查了一下,發現沒有什麼合適的可用資源,所以就自己寫了一個簡單的DEMO。


讀取本地json的方法如下:

+ (NSMutableArray *)getCityData{    NSArray *jsonArray = [[NSArray alloc]init];    NSData *fileData = [[NSData alloc]init];    NSUserDefaults *UD = [NSUserDefaults standardUserDefaults];    if ([UD objectForKey:@"city"] == nil) {        NSString *path;        path = [[NSBundle mainBundle] pathForResource:@"zh_CN" ofType:@"json"];        fileData = [NSData dataWithContentsOfFile:path];                [UD setObject:fileData forKey:@"city"];        [UD synchronize];    }    else {        fileData = [UD objectForKey:@"city"];    }    NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:0];    jsonArray = [NSJSONSerialization JSONObjectWithData:fileData options:NSJSONReadingMutableLeaves error:nil];    for (NSDictionary *dict in jsonArray) {        [array addObject:dict];    }        return array;}



citypicker主要代碼如下:

//返回顯示的列數- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{    if (pickerView == self.cityPicker) {        return 3;    }    else        return 1;}//返回當前列顯示的行數- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{        if (component == 0) {        return [YMUtils getCityData].count;    }    else if (component == 1) {        NSArray *array = [YMUtils getCityData][row1][@"children"];        if ((NSNull*)array != [NSNull null]) {            return array.count;        }        return 0;    }    else {        NSArray *array = [YMUtils getCityData][row1][@"children"];        if ((NSNull*)array != [NSNull null]) {            NSArray *array1 = [YMUtils getCityData][row1][@"children"][row2][@"children"];            if ((NSNull*)array1 != [NSNull null]) {                return array1.count;            }            return 0;        }        return 0;    }}//設定當前行的內容- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{        if(component == 0) {        return [YMUtils getCityData][row][@"name"];    }    else if (component == 1) {        return [YMUtils getCityData][row1][@"children"][row][@"name"];    }    else if (component == 3) {        return [YMUtils getCityData][row1][@"children"][row2][@"children"][row][@"name"];    }    return nil;    }//選擇的行數- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {    if (component == 0) {        row1 = row;        row2 = 0;        row3 = 0;        [self.cityPicker reloadComponent:1];        [self.cityPicker reloadComponent:2];            }    else if (component == 1){        row2 = row;        row3 = 0;        [self.cityPicker reloadComponent:2];            }    else {        row3 = row;    }    NSInteger cityRow1 = [self.cityPicker selectedRowInComponent:0];    NSInteger cityRow2 = [self.cityPicker selectedRowInComponent:1];    NSInteger cityRow3 = [self.cityPicker selectedRowInComponent:2];    NSMutableString *str = [[NSMutableString alloc]init];    [str appendString:[YMUtils getCityData][cityRow1][@"name"]];    NSArray *array = [YMUtils getCityData][cityRow1][@"children"];    if ((NSNull*)array != [NSNull null]) {        [str appendString:[YMUtils getCityData][cityRow1][@"children"][cityRow2][@"name"]];        NSArray *array1 = [YMUtils getCityData][cityRow1][@"children"][cityRow2][@"children"];        if ((NSNull*)array1 != [NSNull null]) {            [str appendString:[YMUtils getCityData][cityRow1][@"children"][cityRow2][@"children"][cityRow3][@"name"]];        }    }    self.cityLabel.text = str;}//每行顯示的文字樣式- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 107, 30)];    titleLabel.textAlignment = NSTextAlignmentCenter;    titleLabel.font = [UIFont systemFontOfSize:14];    titleLabel.backgroundColor = [UIColor clearColor];    if (component == 0) {        titleLabel.text = [YMUtils getCityData][row][@"name"];    }    else if (component == 1) {        titleLabel.text = [YMUtils getCityData][row1][@"children"][row][@"name"];    }    else {        titleLabel.text = [YMUtils getCityData][row1][@"children"][row2][@"children"][row][@"name"];    }    return titleLabel;    }




DEMO的:http://download.csdn.net/detail/u011918080/8200985

iOS國家城市選取器 讀取本地json檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.