需求点:程序员
常见通信录基本功能,按首字母或者汉字拼音首字母分组排序索引。数组
须要解决的问题:this
一、本地化,世界语言那么多,你都认识吗?大部分人都是这样的一种状态吧:它认识我,可我不认识它啊。翻译
二、以中文为例,汉字转换成拼音,而后再排序,想一想都头疼。。code
三、暂时没想到,槽点应该仍是有的,,吧。对象
解决思路:排序
秉持着,能用系统的,就不要本身写的原则,盯着苹果自家的通信录,盯~~~,伪装盯了很久(实际上是去找了下苹果的API文档),终于找到了它——UILocalizedIndexedCollation。索引
一、UILocalizedIndexedCollation的分组排序是创建在对象的操做上的。ip
二、主要的两个API,以下,本身翻译吧。ci
返回传入object对象指定selector在[UILocalizedIndexedCollation currentCollation]中的匹配的索引 // Returns the index of the section that will contain the object. // selector must not take any arguments and return an NSString. - (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector;
经过上面的API筛选完后,将匹配某个索引的数组丢给array,并指定selector,按照selector对这个array进行再次排序,最终返回一个归属某个索引的按照首字母或汉字首字母排序的新数组。 // Used for sorting objects within the same section. // selector must not take any arguments and return an NSString. // In the process of sorting the array, each object may receive // selector multiple times, so this method should be fast. - (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector;
三、使用示例代码
// 一、初始化一个索引,根据不一样国家语言,会初始化出不一样的索引,中文的是“A~Z,#”,供27个,其余语言,本身试试吧。只看得懂中文的说。 UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation]; // 二、初始化一堆数据原对象,这里省略一千万个好友基本信息的对象模型(如:Person,person.name) // 三、获取索引的数量,并初始化对应数量的空数组,用于存放筛选数据 NSInteger sectionTitlesCount = [[collation sectionTitles] count]; // 基本二维数组 NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount]; for (NSInteger index = 0; index < sectionTitlesCount; index++) { NSMutableArray *array = [[NSMutableArray alloc] init]; [newSectionsArray addObject:array]; } // 四、假设好友对象是Person,自带属性name for (Person *p in srcArray) { //获取name属性的值所在的位置,好比"小白鼠",首字母是X,在A~Z中排第23(第一位是0),sectionNumber就为23 NSInteger sectionNumber = [collation sectionForObject:p collationStringSelector:@selector(name)]; //把name为“小白鼠”的p加入newSectionsArray中的第23个数组中去 NSMutableArray *sectionNames = newSectionsArray[sectionNumber]; [sectionNames addObject:p]; } // 五、对每一个section中的数组按照name属性排序 for (NSIntger i = 0; i < sectionTitlesCount; i++) { NSMutableArray *personArrayForSection = newSectionsArray[i]; NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)]; newSectionsArray[i] = sortedPersonArrayForSection; } // 六、最后能够过滤下不存在数据的索引,省略
四、应用到一个tableview上
// 按照索引个数配置tableview区数 - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { return [[UILocalizedIndexedCollation currentCollation] sectionTitles][section]; } // 配置索引内容,就是通信录中右侧的那一列“A~Z、#” - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles]; } // 索引点击响应 - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index { return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index]; }
UILocalizedIndexedCollation没法区分汉语中的多音字。毕竟中文博大精深嘛。