被忽略的UILocalizedIndexedCollation

关键字:字母表,按首字母排序,索引表

遇到开发好友列表,产品要求好友列表按字母排序,本人刚参与工作,只能google或者百度,借鉴大神。结果搜到的是一堆第三方框架,大神的轮子,然而这并不是我想要的,只能翻一下苹果官方文档,不负有心人,找到了神器UILocalizedIndexedCollation。

1、理论

注:section title 和section index title 是可以不相同的。

2、实战

接下来就讲讲它的具体使用,三部曲:

  • 1、获取单例的collation
  • 2、分组(分类)
  • 3、排序
   1、获取单例的collation
    UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];
    
    //标题数组
    NSInteger sectionTitlesCount = [[collation sectionTitles] count];
    //设置sections数组初始化:元素包含userObjs数据的空数据
    NSMutableArray *newSectionsArray = [[NSMutableArray alloc] initWithCapacity:sectionTitlesCount];
    for (NSInteger index = 0; index < sectionTitlesCount; index++) {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        [newSectionsArray addObject:array];
    }
    
    2、分类 - 将用户数据进行分类,存储到对应的sesion数组中
    for (MTDUserBaseModel *p in self.friendListRequest.friendMutableArray) {
        NSInteger sectionNumber = [collation sectionForObject:p collationStringSelector:@selector(userNickname)];
        NSMutableArray *sectionNames = newSectionsArray[sectionNumber];
        [sectionNames addObject:p];
   }
    
    3、排序 - 对每个已经分类的数组中的数据进行排序
    for (NSInteger index = 0; index < sectionTitlesCount; index++) {
         NSMutableArray *personArrayForSection = newSectionsArray[index];
         NSArray *sortedPersonArrayForSection = [collation sortedArrayFromArray:personArrayForSection collationStringSelector:@selector(userNickname)];
        newSectionsArray[index] = sortedPersonArrayForSection;
    }

参考:
1、苹果示例代码
2、UILocalized​Indexed​Collation

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

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,403评论 19 139
  • 如果您想在UITableView中显示大量的记录,则最好重新考虑显示数据的方式。随着行数的增加,table vie...
    刘铭iOS阅读 8,708评论 0 1
  • 来源:NumPy Tutorial - TutorialsPoint 译者:飞龙 协议:CC BY-NC-SA 4...
    布客飞龙阅读 33,255评论 6 98
  • 1、窗体 1、常用属性 (1)Name属性:用来获取或设置窗体的名称,在应用程序中可通过Name属性来引用窗体。 ...
    Moment__格调阅读 10,040评论 0 11
  • 是不是因为有愧于心 所以抱得太紧 会不会自己都不相信 才要强调真实 越心虚越解释 关于你和她公开的秘密 没什么了不...
    请别说对不起阅读 1,131评论 0 0