iOS-(通讯录、地区)排序神器——UILocalizedIndexedCollation

有的app中有通讯录或者地区等信息,需要按照字母表进行A-Z的排序,在认识UILocalizedIndexedCollation神器之前,我的做法是利用字符串转化后排序,后来偶然发现,其实系统已经自带了排序类——UILocalizedIndexedCollation,效果如图:

Paste_Image.png

上关键部分代码:


-(void)setUpTableSection{
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
    
    //create a temp sectionArray
    NSUInteger numberOfSections = [[collation sectionTitles] count];
    NSMutableArray *newSectionArray = [[NSMutableArray alloc]init];
    for (NSUInteger index = 0; index < numberOfSections; index++) {
        [newSectionArray addObject:[[NSMutableArray alloc]init]];
    }
    
    // insert Persons info into newSectionArray
    for (RRZGoodFriendItem *item in self.friends) {
        NSUInteger sectionIndex;
        if (item.nameNotes) {
            sectionIndex = [collation sectionForObject:item collationStringSelector:@selector(nameNotes)];  //备注名
        }else{
            sectionIndex = [collation sectionForObject:item collationStringSelector:@selector(custNname)];  //昵称
        }
        [newSectionArray[sectionIndex] addObject:item];
    }
    
    //sort the person of each section
    for (NSUInteger index=0; index < numberOfSections; index++) {
        NSMutableArray *personsForSection = newSectionArray[index];
        NSArray *sortedPersonsForSection = [collation sortedArrayFromArray:personsForSection collationStringSelector:@selector(custNname)];
        newSectionArray[index] = sortedPersonsForSection;
    }
    
    NSMutableArray *temp = [NSMutableArray new];
    self.sectionTitlesArray = [NSMutableArray new];
    
    [newSectionArray enumerateObjectsUsingBlock:^(NSArray *arr, NSUInteger idx, BOOL *stop) {
        if (arr.count == 0) {
            [temp addObject:arr];
        } else {
            [self.sectionTitlesArray addObject:[collation sectionTitles][idx]];
        }
    }];
    
    [newSectionArray removeObjectsInArray:temp];
    
    NSMutableArray *operrationModels = [NSMutableArray new];
    NSArray *dicts = @[@{@"custNname" : @"关注我的", @"headPath" : @"r_address_newFriend"},
                       @{@"custNname" : @"我的关注", @"headPath" : @"r_address_regard"},
                       @{@"custNname" : @"群聊", @"headPath" : @"r_address_group"},
                       @{@"custNname" : @"黑名单", @"headPath" : @"r_address_blackList"}];
    
    for (NSDictionary *dic in dicts) {
        RRZGoodFriendItem *item = [RRZGoodFriendItem new];
        item.custNname = dic[@"custNname"];
        item.headPath = dic[@"headPath"];
        [operrationModels addObject:item];
    }
    
    [newSectionArray insertObject:operrationModels atIndex:0];
    [self.sectionTitlesArray insertObject:@"" atIndex:0];
    
    self.nFriends = newSectionArray;
}

这次没有上全部代码。若有疑问,欢迎留言。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,173评论 25 709
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,902评论 18 399
  • 守望初心 人的一生有很多梦想,而真正坚持住自己最初的梦想,而为之不懈努力的人,...
    上下Ud阅读 1,898评论 0 0
  • 一 细细数来,在简书已经有两个月了。 陆续的在简书上发表了几篇文字,除了一篇阅读量突破了四百,其余几篇均在一百上下...
    椿树下阅读 3,456评论 14 16
  • 这是一个信息爆炸的时代,每天看手机的时间,几乎占据首位。可是,看手机能让心静下来吗?不能。 那么,当心静下来的时候...
    德合阅读 2,890评论 0 3