美文网首页Func
iOS-(通讯录、地区)排序神器——UILocalizedInd

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

作者: Super_Yi | 来源:发表于2016-06-22 16:58 被阅读973次

有的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;
}

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

相关文章

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

    有的app中有通讯录或者地区等信息,需要按照字母表进行A-Z的排序,在认识UILocalizedIndexedCo...

  • 通讯录排序

    既然有通讯录,当然离不开排序 排序标记 外部调用接口 /** 分组排序 @param objects...

  • iOS-排序算法

    传送门 iOS-冒泡排序(Bubble Sort)[https://www.jianshu.com/p/e2eef...

  • iOS-数组排序

    首先提供一些排序文章供大家参考学习常用排序算法总结iOS-八大基本排序Sort 各类算法和时间复杂度分析 关于iO...

  • iOS-排序

    高级排序:按描述进行排序(制定一套排序规则 ) 4 字典排序 5 简单测试代码

  • BAContact:通讯录,最简单的中英文混合排序封装,微信通讯

    BAContact 1、功能及简介 1、通讯录,最简单的中英文混合排序封装,微信通讯录 demo! 2、获取系统通...

  • iOS通讯录开发

    前言 记录下在应用内获取通讯录的相关知识点,涉及到通讯录权限配置,通讯录数据的获取以及数据按首字母排序。 系统通讯...

  • ios-数组排序

    需求: 需要对数组noReadArray按照最后的时间lastTimej进行排序 实现方式: 使用场景: 1、需要...

  • iOS-中文排序

    中文排序想想就心累,之前尝试过很多方法也用过某些框架,但是效果并不好。 偶然看到一个方法 输出为: 中文转拼音的方...

  • iOS-排序,比较

    BOOL result=[acompare:b]; if(result==NSOrderedSame){ a= b...

网友评论

本文标题:iOS-(通讯录、地区)排序神器——UILocalizedInd

本文链接:https://www.haomeiwen.com/subject/spkndttx.html