php 通讯录排序,UILocalizedIndexedCollation——本地化索引排序

独爱系统API,做一个拒绝自定义功能的程序员,产品的奇葩需求都一边去吧

UILocalizedIndexedCollation——本地化索引排序

需求点:

常见通讯录基本功能,按首字母或者汉字拼音首字母分组排序索引。

需要解决的问题:

1、本地化,世界语言那么多,你都认识吗?大部分人都是这样的一种状态吧:它认识我,可我不认识它啊。

2、以中文为例,汉字转换成拼音,然后再排序,想想都头疼。。

3、暂时没想到,槽点应该还是有的,,吧。

解决思路:

秉持着,能用系统的,就不要自己写的原则,盯着苹果自家的通讯录,盯~~~,假装盯了好久(其实是去找了下苹果的API文档),终于找到了它——UILocalizedIndexedCollation。

很认真地说:上面都是废话,字数:251。是的,不是250,是251,咱多少要有那么一点点的不同。

1、UILocalizedIndexedCollation的分组排序是建立在对象的操作上的。

2、主要的两个API,如下,自己翻译吧。

返回传入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;

3、使用示例代码

// 1、初始化一个索引,根据不同国家语言,会初始化出不同的索引,中文的是“A~Z,#”,供27个,其他语言,自己试试吧。只看得懂中文的说。

UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];

// 2、初始化一堆数据原对象,这里省略一千万个好友基本信息的对象模型(如:Person,person.name)

// 3、获取索引的数量,并初始化对应数量的空数组,用于存放筛选数据

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];

}

// 4、假设好友对象是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];

}

// 5、对每个section中的数组按照name属性排序

for (NSIntger i = 0; i < sectionTitlesCount; i++) {

NSMutableArray *personArrayForSection = newSectionsArray[i];

NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];

newSectionsArray[i] = sortedPersonArrayForSection;

}

// 6、最后可以过滤下不存在数据的索引,省略

4、应用到一个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];

}

At Last

UILocalizedIndexedCollation无法区分汉语中的多音字。毕竟中文博大精深嘛。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值