ios选择城市

来源:互联网 发布:股票交易软件制作 编辑:程序博客网 时间:2024/04/28 17:00

在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