iOS国家城市选择器 读取本地json文件
2014-11-26 16:56
531 查看
最近在做的产品有这么个需求,读取本地json文件中的国家和城市信息,显示到pickerview上,在网上查了一下,发现没有什么合适的可用资源,所以就自己写了一个简单的DEMO。
效果图:
读取本地json的方法如下:
citypicker主要代码如下:
DEMO的下载地址:http://download.csdn.net/detail/u011918080/8200985
效果图:
读取本地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/plist 文件
- iOS 读取本地JSON文件
- ios本地文件内容读取json .plist 文件读取
- ios本地文件内容读取,.json .plist 文件读写
- iOS - 关于xcode读取本地 json 文件的demo
- ios-本地项目下 json文件的读取
- iOS Swift读取本地json文件报错的解决方法
- ios本地文件读取,json解析YYModel
- iOS 读取本地Json文件
- ios本地文件内容读取,.json .plist 文件读写
- jQuery的getJSON方法不支持本地文件读取,一定要在服务器当中
- 关于iOS中将json格式存到文件中并读取解析成字典
- Flex中读取(嵌入)本地JSON文件
- 从本地读取json文件
- cocos2dx LUA 读取本地JSON文件
- 【iOS开发问题】使用NSCoding写入本地文件读取不出来或者文件没有写入本地
- java 读取本地的json文件
- IOS本地读取HTML文件内容 适合新手试试
- ios开发中读取本地的plist文件
- Unity3D研究院之IOS Android支持中文与本地文件的读取写入