ios选择城市
来源:互联网 发布:90年代中国电影 知乎 编辑:程序博客网 时间:2024/04/29 02:01
ios选择城市
在ios开发当中,选择城市是很常用的,一般都是根据城市的拼音首字母进行分组,然后用分组的UITableView展现出来,实现的效果如下
我是使用了两个plist文件存储城市的,一个是存储所有的城市,另外一个是存储热门城市
存储所有的城市的city.plist,只是存取城市的中文名称而已,之后用一个第三方的开源库ChineseToPinyin来把中文转换成拼音,然后根据拼音的首字母就可以分组了
存储热门城市的hotCity.plist,也只是存储中文名称,之后把这一组直接归为热门这一组就可以了
下面讲解一下具体操作
第一步就是直接读取了,这一步不用多说了
1 hotcityarray=[[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"hotCity" ofType:@"plist"]];2 3 cityarray=[[NSMutableArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"city" ofType:@"plist"]];
第二步就是创建个NSMutableDictionary,key就是字母,value就是城市拼音首字母是key的所有城市
cityDic=[[NSMutableDictionary alloc]init]; NSString * pinyin=nil; NSMutableArray *arr=nil; for (NSString * city in cityarray) { pinyin=[[ChineseToPinyin pinyinFromChiniseString:city] substringToIndex:1]; //如果包含key if([[cityDic allKeys]containsObject:pinyin]){ arr=[cityDic objectForKey:pinyin]; [arr addObject:city]; [cityDic setObject:arr forKey:pinyin]; }else{ arr= [[NSMutableArray alloc]initWithObjects:city, nil]; [cityDic setObject:arr forKey:pinyin]; } }
第三步就是把热门城市加进去
sortArray=[[NSMutableArray alloc]initWithObjects:@"热门", nil]; sortArray= [sortArray arrayByAddingObjectsFromArray:[[cityDic allKeys] sortedArrayUsingSelector:@selector(compare:)]]; [cityDic setObject:hotcityarray forKey:@"热门"];
这样数据处理就基本完成了
下一步就是界面展现了
#pragma mark Table View Data Source Methods//选中- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row]; NSMutableArray *array=[tableViewDic objectForKey:[sortArray objectAtIndex:section]]; NSLog(@"%@",[array objectAtIndex:row]); [[NSUserDefaults standardUserDefaults]setObject:[array objectAtIndex:row] forKey:@"city"]; [[NSUserDefaults standardUserDefaults]synchronize]; [self back:nil]; }- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { //这个方法用来告诉表格有几个分组 return [sortArray count];}- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { //这个方法告诉表格第section个分组有多少行 return [[tableViewDic objectForKey:[sortArray objectAtIndex:section]]count];}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //这个方法用来告诉某个分组的某一行是什么数据,返回一个UITableViewCell NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row]; static NSString *GroupedTableIdentifier = @"cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: GroupedTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:GroupedTableIdentifier]; } NSMutableArray *array=[tableViewDic objectForKey:[sortArray objectAtIndex:section]]; //给Label附上城市名称 key 为:C_Name cell.textLabel.text = [array objectAtIndex:row]; cell.textLabel.font = [UIFont fontWithName:@"TrebuchetMS-Bold" size:15]; return cell;}- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { //这个方法用来告诉表格第section分组的名称 return [sortArray objectAtIndex:section];}- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ return 40.0f;}- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { //返回省份的数组 return sortArray;}
0 0
- ios pickerview选择城市
- ios选择城市
- iOS开发-选择城市
- ios选择城市
- 城市选择
- 城市选择
- IOS UIPickView+sqlite 选择中国所有城市案例
- IOS UIPickView+sqlite 选择中国所有城市案例
- iOS tableview cell下拉展开显示详情选择城市
- 【代码笔记】iOS-先选择城市,然后,跳转Tabbar
- 高仿iOS 滚轮实现 省市区 城市选择三级联动
- 城市选择js效果
- 省份 城市 选择列表
- 城市选择表单代码
- 二级联动城市选择
- UIPickerView + 城市选择
- 城市选择功能实现
- UIPickerView城市选择
- linux sed 命令总结
- 51node-1449 砝码称重(进制思想)
- 自动化测试 概述
- 面试的逻辑题
- SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
- ios选择城市
- LintCode A + B 问题 : 理解不使用运算符计算加乘
- 这3个技巧,能帮你节省50%的工作和学习时间!
- 为什么使用koa2搭建微信第三方公众平台
- hello,world
- 【Leetcode】之 Binary Tree Level Order Traversal II
- 微信公众平台开发(120) 微信支付报关接口
- 图形界面笔记
- Hackerearth: August Circuits 比赛记录