在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; }
然后再增加個UISearchBar用於搜索城市,這樣就搞定了