美文网首页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

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