本地化下按首字母分组排序的神器——UILocalizedIndexedCollation

最近在整一个很简单的通讯录相关的项目,通讯录当然就少不了按首字母或者汉字拼音首字母分组排序索引。因为按照我一贯的的做法,都是想要做成更通用的、支持本地化的,所以这就纠结了,世界各地的语言啊我去,我顶多也就认识中文和英语,这就不能用以前的那些比如把汉字转成拼音再排序的方法了,效率不高不说,对其他国家的本地化更是行不通。一个偶然的机会,我才发现SDK里已经提供了一个实现此功能的神器——UILocalizedIndexedCollation。

  首先提一下,UILocalizedIndexedCollation的分组排序是建立在对对象的操作上的。下边我会举个栗子讲解一下。首先已知有一个Person类:

 @interface Person : NSObject
 @property(nonatomic, strong) NSString *name;
 @end

然后初始化一些对象存入一个数组(注:为了后续说明方便,我直接拿name的值来表示Person类的对象,实际编码中是要用对象!如下列<林丹>表示p.name = @"林丹"的Person类对象p

NSArray *srcArray = @[<林荣>, <林丹>, <周董>, <周树人>, <周杰伦>, <阿华>];

先将UILocalizedIndexedCollation初始化,

UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];

可以看出来这是一个单例,它会根据不同国家的语言初始化出不同的结果。如中文和英文的得到的就是A~Z和#,日语的就是A~Z,あ, か, さ, た, な, は, ま, や, ら, わ和#。下边我就以最熟悉的中文环境为例,直接上代码了,注意看注释部分的讲解

//得出collation索引的数量,这里是27个(26个字母和1个#)
NSInteger sectionTitlesCount = [[collation sectionTitles] count];

//初始化一个数组newSectionsArray用来存放最终的数据,我们最终要得到的数据模型应该形如@[@[以A开头的数据数组], @[以B开头的数据数组], @[以C开头的数据数组], ... @[以#(其它)开头的数据数组]]

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 *p in srcArray) {
  //获取name属性的值所在的位置,比如"林丹",首字母是L,在A~Z中排第11(第一位是0),sectionNumber就为11
    NSInteger sectionNumber = [collation sectionForObject:p collationStringSelector:@selector(name)];
  //把name为“林丹”的p加入newSectionsArray中的第11个数组中去
    NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
    [sectionNames addObject:p]; 
}
    
//对每个section中的数组按照name属性排序
for (NSIntger index = 0; index < sectionTitlesCount; index++) {
    NSMutableArray *personArrayForSection = newSectionsArray[index];
    NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(name)];
    newSectionsArray[index] = sortedPersonArrayForSection;
}



最终把newSectionsArray应该形如@[@[<阿华>], @[], @[], ... @[<林丹>, <林荣>], ... @[<周董>, <周杰伦>, <周树人>], @[]]

后续工作就是把这个数组作为数据源与UITableView通过tableView的Delegate关联起来了,部分如下,在此不再赘述

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return [collation sectionTitles][section];
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [collation sectionIndexTitles];
}

- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index {
    return [collation sectionForSectionIndexTitleAtIndex:index];
}


  不过呢,使用这个UILocalizedIndexedCollation有一个缺点就是不能区分姓氏中的多音字,比如“曾”会被分到"C"组去,不知道大家有没有基于此的好方法在下边回复。下边是苹果官方示例,其中第3个是关于UILocalizedIndexedCollation的,可以下载下来学习一下http://developer.apple.com/library/ios/samplecode/TableViewSuite/Introduction/Intro.html



http://www.cnblogs.com/ningxu-ios/p/4183542.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值