UILocalizedIndexedCollation 是一个帮助我们组织列表数据的类,它能够根据地区来生成与之对应区域索引标题。不需要直接创建它的对象,我们可以通过 UILocalizedIndexedCollation +currentCollation 获得一个对应当前地区的单例对象。
项目地址:https://github.com/MisterZhouZhou/UISearchControllerAndUILocalizedCollection
简单操作如下:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles]count];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *ID = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
}
cell.textLabel.text = @"cell";
return cell;
}
- (NSString *)tableView:(UITableView *)tableView
titleForHeaderInSection:(NSInteger)section
{
return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}
- (NSInteger)tableView:(UITableView *)tableView
sectionForSectionIndexTitle:(NSString *)title
atIndex:(NSInteger)index
{
return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}
效果图:
下面开始对汉姓进行排序:
1、初始化数据:
//配置数据源
NSArray *firstNameArray = @[@"赵",@"钱",@"孙",@"李",@"周",@"吴",@"郑",@"王",@"郭",@"松",@"宋",@"长",@"大",@"小"];
NSMutableArray *tempArray = [NSMutableArray array];
for (int i=0; i<firstNameArray.count; i++) {
Person *p = [Person new];
p.name = [NSString stringWithFormat:@"%@",firstNameArray[i]];
[tempArray addObject:p];
}
2、对数据进行排序筛选
//初始化UILocalizedIndexedCollation
localizedCollection = [UILocalizedIndexedCollation currentCollation];
//得出collation索引的数量,这里是27个(26个字母和1个#)
NSInteger sectionTitlesCount = [[localizedCollection sectionTitles] count];
//初始化一个数组newSectionsArray用来存放最终的数据
NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
//初始化27个空数组加入newSectionsArray
for (NSInteger index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *array = [[NSMutableArray alloc] init];
[newSectionsArray addObject:array];
}
//将每个人按name分到某个section下
for (Person *temp in tempArray) {
//获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11
NSInteger sectionNumber = [localizedCollection sectionForObject:temp collationStringSelector:@selector(name)];
NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
[sectionNames addObject:temp];
}
//对每个section中的数组按照name属性排序
for (int index = 0; index < sectionTitlesCount; index++) {
NSMutableArray *personArrayForSection = newSectionsArray[index];
NSArray *sortedPersonArrayForSection = [localizedCollection sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];
newSectionsArray[index] = sortedPersonArrayForSection;
}
//section title
sectionTitleArray = [NSMutableArray array];
NSMutableArray *tempArr = [NSMutableArray array];
[newSectionsArray enumerateObjectsUsingBlock:^(NSArray *array, NSUInteger idx, BOOL * _Nonnull stop) {
if (array.count == 0) {
[tempArr addObject:array];
}else{
[sectionTitleArray addObject:[localizedCollection sectionTitles][idx]];
}
}];
[newSectionsArray removeObjectsInArray:tempArr];
dataArray = newSectionsArray.copy;
效果图: