第一步就是直接讀取了,這一步不用多說了
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; }