UILocalizedIndexedCollation可以实现区域排序,类似通讯录的样式。
//首先进行初始化
locationCollation = [UILocalizedIndexedCollation currentCollation];
//获取section标题 A-Z#
array_collation_title =[[NSMutableArray alloc]initWithArray:[locationCollation sectionTitles]];
//构建每个section数组 array_section =[[NSMutableArray alloc]init]; for (int i=0; i<array_collation_title.count; i++) { NSMutableArray *subArray = [[NSMutableArray alloc]init]; [array_section addObject:subArray]; } //排序 //排序对象,放进分区数组中 for (Person *person in array_datas) { NSInteger section = [locationCollation sectionForObject:person collationStringSelector:@selector(name)]; NSMutableArray *subarray =array_section[section]; [subarray addObject:person]; } //分别对分区进行排序 for (int i =0;i<array_section.count;i++) { NSMutableArray *subarray = [array_section objectAtIndex:i]; NSArray *sortArr = [locationCollation sortedArrayFromArray:subarray collationStringSelector:@selector(name)]; [array_section replaceObjectAtIndex:i withObject:sortArr]; }
#pragma mark -datasouth //返回section 要显示的标题集合 -(NSArray<NSString *>*)sectionIndexTitlesForTableView:(UITableView *)tableView{ // return [[UILocalizedIndexedCollation currentCollation]sectionTitles]; return array_collation_title; } //根据section 返回显示的标题 -(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{ // return [[[UILocalizedIndexedCollation currentCollation]sectionTitles]objectAtIndex:section]; return [array_collation_title objectAtIndex:section]; } //点击右侧字母,触发此方法,告诉数据源 选中的section -(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{ return [[UILocalizedIndexedCollation currentCollation]sectionForSectionIndexTitleAtIndex:index]; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ int count = 0; for (NSMutableArray *subArr in array_section) { if (subArr.count>0) { count++; } } return count; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ NSMutableArray *subArr = [array_section objectAtIndex:section]; return subArr.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"]; if (cell==nil) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"]; } NSMutableArray *subArray = [array_section objectAtIndex:indexPath.section]; Person *person =[subArray objectAtIndex:indexPath.row]; if (person!=nil) { [cell.textLabel setText:person.name]; } return cell; } -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{ NSLog(@"section = %d,row = %d",indexPath.section,indexPath.row); } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{ return 15; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ return 15; }
运行效果图
运行后发现 在 没有值的section下 依然显示section标题,这样会感觉非常丑。
优化一下代码,
添加以下方法
//删除没有值的subArr for (int i =0;i<array_section.count;i++) { NSMutableArray *subarray = [array_section objectAtIndex:i]; if (subarray.count==0) { [array_section removeObjectAtIndex:i]; [array_collation_title removeObjectAtIndex:i]; i--; // 这里很重要 } }