iOS仿微信联系人界面中联系人按姓名首字母分类UILocalizedIndexedCollation

来源:互联网 发布:初中物理知识网络 编辑:程序博客网 时间:2024/04/30 03:45

字母分组排序神器:UILocalizedIndexedCollation

- (void)configureTableViewSection {        // 实例化    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];        // [collation sectionTitles]得到一个包含(A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,#)的数组    // numberOfSections为27    NSUInteger numberOfSections = [[collation sectionTitles] count];        // 初始化一个sectionMutableArray来存放数据,其27个元素都是NSMutableArray    NSMutableArray *sectionMutableArray =  [[NSMutableArray alloc] init];    for (NSUInteger index = 0; index < numberOfSections; index ++) {        [sectionMutableArray addObject:[[NSMutableArray alloc] init]];    }        /*     * 假设现在有个一个数据模型DataModel类,DataModel类里面有字段nameStr、avatarStr,生成若干DataModel的实例已经加入到了数组(NSMutableArray)dataMutableArray中     */    for (DataModel *oneDataModel in dataMutableArray) {        // 获取nameStr首字母所在的位置        NSUInteger sectionIndex = [collation sectionForObject:oneDataModel collationStringSelector:@selector(nameStr)];        [sectionMutableArray[sectionIndex] addObject:oneDataModel];    }        // 对分成一类的数组进行排序    for (NSUInteger index = 0; index < numberOfSections; index ++) {        // 获取某一字母分类中得数组        NSMutableArray *indexSectionMutableArray = sectionMutableArray[index];        // 以nameStr字段排序        NSArray *sortedSectionArray = [collation sortedArrayFromArray:indexSectionMutableArray collationStringSelector:@selector(nameStr)];        sectionMutableArray[index] = sortedSectionArray;    }        NSMutableArray *emptyMutableArray = [NSMutableArray new];    NSMutableArray *sectionTitleMutableArray = [NSMutableArray new];        [sectionMutableArray enumerateObjectsUsingBlock:^(NSArray *arrary, NSUInteger idx, BOOL *stop) {                if (arrary.count == 0) {            // 可能有很多arrary.count为0的,把这些arrary加到emptyMutableArray里,然后从sectionMutableArray里移除            [emptyMutableArray addObject:arrary];        }        else {            // section标题,如A,B,C...            [sectionTitleMutableArray addObject:[collation sectionTitles][idx]];        }    }];        [sectionMutableArray removeObjectsInArray:emptyMutableArray];        NSMutableArray *showMutableArray = [[NSMutableArray alloc] init];    // 自己加图片    NSArray *dictArray = @[@{@"nameStr": @"新的朋友", @"imageNameStr": @""},                         @{@"nameStr": @"群聊", @"imageNameStr": @""},                         @{@"nameStr": @"标签", @"imageNameStr": @""},                         @{@"nameStr": @"公众号", @"imageNameStr": @""}];        for (NSDictionary *oneDict in dictArray) {        /*         * 假设现在有个一个数据模型ShowModel类,ShowModel类里面有字段nameStr、imageNameStr         */        ShowModel *showModel = [ShowModel new];        showModel.nameStr = oneDict[@"nameStr"];        showModel.imageNameStr = oneDict[@"imageNameStr"];                [showMutableArray addObject:showModel];    }        // 第一个section显示自定义的    [sectionMutableArray insertObject:showMutableArray atIndex:0];    // sectionTitleMutableArray存放的是section的标题A,B,C...,第一个section不需要section title    [sectionTitleMutableArray insertObject:@"" atIndex:0];        // 把sectionMutableArray、sectionTitleMutableArray全局化,作为tableView的数据源    self.sectionMutableArray = sectionMutableArray;    self.sectionTitleMutableArray = sectionTitleMutableArray;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 农业银行卡多次输错密码怎么办 想把店长弄走怎么办 济南银座卡过期了怎么办 银座购物卡丢失后怎么办 银座的卡丢了怎么办 银行卡换了旧卡怎么办 大理市民卡丢了怎么办 市民卡内的钱怎么办 宝付支付乱扣款怎么办 苏宁任性贷逾期怎么办 第二次跟家里开口要钱还网贷怎么办 网贷到家来要钱怎么办 网贷贷不了啦急要钱怎么办 百度推广竞价关键词太长怎么办 药店位置差客流少怎么办 网站上的用词违规怎么办 苹果16g内存不够怎么办 手机16g内存不够怎么办 在私企年纪大了怎么办 谷歌浏览器显示不安全打不开怎么办 4s密码多次错误怎么办 苹果4s手机系统错误怎么办 汽车充电口坏了怎么办 如果手机充不了电怎么办 淘宝买的家电坏了怎么办 衣服皱了没有熨斗怎么办 油烟机油盒坏了怎么办 实体店不给换货怎么办 台式电脑鼠标不动了怎么办 电脑开机一直长鸣报警怎么办 国外电话卡网速太慢怎么办 滴滴提现忘记登录密码怎么办 微信提现支付密码忘记了怎么办 小米手机前置摄像头打不开怎么办 mp4视频屏幕好小怎么办 投了简历没回应怎么办 借壳上市原股票怎么办 红米note3指纹解锁失灵怎么办 来分期刷脸失败怎么办 买到假货商家不承认怎么办 买到假货翻新货工商局怎么办