关于通讯录的项目就会有按首字母或者汉字拼音首字母分组排序索引。说说以前用的就是 比如把汉字转成拼音再排序的方法了,不仅效率低,对其他语言的本地化更是行不通。偶然间在阅读别人的代码时发现了UILocalizedIndexedCollation,于是搜索相关资料,整理了一下。参考自文章http://nshipster.cn/uilocalizedindexedcollation/
UILocalizedIndexedCollation的分组排序是建立在对对象的操作上的。如有一个Person类:
@interface Person : NSObject
@property (nonatomic, copy) NSString *name;
+ (instancetype)modelWithObj:(NSString *)obj;
@end
@implementation Person
+ (instancetype)modelWithObj:(NSString *)obj {
Person *model = [Person new];
model.name = obj;
return model;
}
@end
下边就以最熟悉的语言环境为例:
- (NSMutableArray *)setObjects:(NSMutableArray *)objects {
SEL selector = @selector(name);
UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
// 得到collation索引数量(26个字母和1个#)
NSInteger sectionTitlesCount = [[collation sectionTitles] count];
// 初始化数组mutableSections用来存放最终数据,我们最终要得到的数据模型应该形如 \
@[@[以A开头的数据数组], @[以B开头的数据数组], ... @[以#(其它)开头的数据数组]]
NSMutableArray *mutableSections = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
// 初始化27个空数组加入mutableSections
for (NSInteger idx = 0; idx < sectionTitlesCount; idx++) {
[mutableSections addObject:[NSMutableArray array]];
}
// 将每个人按name分到某个section下
for (id obj in objects) {
// 获取name属性的值所在的位置,比如“林”首字母是L,在A~Z中排第11,sectionNumber则为11
NSInteger sectionNumber = [collation sectionForObject:obj collationStringSelector:selector];
// name为“林”的obj则加入mutableSections中的第11个数组中
[[mutableSections objectAtIndex:sectionNumber] addObject:obj];
}
// 对每个section中的数组按照name属性排序
for (NSUInteger idx = 0; idx < sectionTitlesCount; idx++) {
NSArray *objsForSection = [mutableSections objectAtIndex:idx];
[mutableSections replaceObjectAtIndex:idx withObject:[collation sortedArrayFromArray:objsForSection collationStringSelector:selector]];
}
return mutableSections;
}
// 将数据源数组打包成模型对象
- (NSMutableArray *)initialDataSources {
NSArray *array = @[@"李娜", @"林丹", @"张学友", @"孙燕姿", @"Sammi", @"Tanya", @"东野圭吾", @"周树人", @"张大千", @"阿新"];
NSMutableArray *dataSources = [NSMutableArray array];
for (id obj in array) {
[dataSources addObject:[Person modelWithObj:obj]];
}
return dataSources;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableArray *willDeleObjs = @[].mutableCopy;
NSMutableArray *reservedObjs = @[].mutableCopy;
NSMutableArray *newSectionsArray = [self setObjects:[self initialDataSources]];
[newSectionsArray enumerateObjectsUsingBlock:^(NSArray *arr, NSUInteger idx, BOOL *stop) {
if (arr.count == 0) {
[willDeleObjs addObject:arr];
} else {
[reservedObjs addObject:[[UILocalizedIndexedCollation currentCollation] sectionTitles][idx]];
}
}];
// 我们可以去除掉mutableSections中空的数组
[newSectionsArray removeObjectsInArray:willDeleObjs];
NSLog(@"reservedObjs - %@", reservedObjs);
// 打印出排序后的name
[newSectionsArray enumerateObjectsUsingBlock:^(NSArray *arr, NSUInteger idx, BOOL * _Nonnull stop) {
for (Person *p in arr) {
NSLog(@"%@", p.name);
}
}];
}
reservedObjs - (
A,
D,
L,
S,
T,
Z
)
ps:使用UILocalizedIndexedCollation有个缺点就是不能区分姓氏中的多音字,比如“曾”会被分到"C"组去,希望大家有好的方法在下边告知