UITableView在行数相当多的时候,给人的感觉是非常笨重的。通常为了方便用户使用,采用的方法有:搜索框、按层级展示、区域索引标题。

  前两种就不用介绍了,此文就介绍区域索引标题的实现。

  区域索引标题可以在通讯录里看到,类似这样:

区域索引标题可以通过转拼音实现,本文主要介绍使用UILocalizedIndexedCollation实现区域索引标题

UILocalizedIndexedCollation是苹果贴心为开发者提供的排序工具,会自动根据不同地区生成索引标题

//根据SEL方法返回的字符串判断对象应该处于哪个分区
- (NSInteger)sectionForObject:(id)object collationStringSelector:(SEL)selector;//根据SEL方法返回的string对数组元素排序
- (NSArray *)sortedArrayFromArray:(NSArray *)array collationStringSelector:(SEL)selector;

具体使用方法如下:

1.构造数据源

NSArray *testArr = @[@"悟空",@"沙僧",@"八戒", @"吴进", @"悟能", @"唐僧", @"诸葛亮", @"赵子龙",@"air", @"Asia", @"crash", @"basic", @"阿里郎"];NSMutableArray *personArr = [NSMutableArray arrayWithCapacity:testArr.count];for (NSString *str in testArr) {Person *person = [[Person alloc] initWithName:str];[personArr addObject:person];}UILocalizedIndexedCollation *collation = [UILocalizedIndexedCollation currentCollation];NSLog(@"%@", collation.sectionTitles);//1.获取获取section标题NSArray *titles = collation.sectionTitles;//2.构建每个section数组NSMutableArray *secionArray = [NSMutableArray arrayWithCapacity:titles.count];for (int i = 0; i < titles.count; i++) {NSMutableArray *subArr = [NSMutableArray array];[secionArray addObject:subArr];}//3.排序//3.1 按照将需要排序的对象放入到对应分区数组for (Person *person in personArr) {NSInteger section = [collation sectionForObject:person collationStringSelector:@selector(name)];NSMutableArray *subArr = secionArray[section];[subArr addObject:person];}//3.2 分别对分区进行排序for (NSMutableArray *subArr in secionArray) {NSArray *sortArr = [collation sortedArrayFromArray:subArr collationStringSelector:@selector(name)];[subArr removeAllObjects];[subArr addObjectsFromArray:sortArr];}

2.实现TableViewDataSource

#pragma mark SectionTitles
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{return [[[UILocalizedIndexedCollation currentCollation] sectionTitles] objectAtIndex:section];
}- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{return [[UILocalizedIndexedCollation currentCollation] sectionIndexTitles];
}- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{return [[UILocalizedIndexedCollation currentCollation] sectionForSectionIndexTitleAtIndex:index];
}

demo地址:https://github.com/WuKongCoo1/UILocalizedIndexedCollationDemo

转载于:https://www.cnblogs.com/pretty-guy/p/4831586.html

iOS 使用UILocalizedIndexedCollation实现区域索引标题(Section Indexed Title)即拼音排序...相关推荐

  1. <nav>导航标签 和div标签一样,块属性标签</nav>    <main>内容区域,和section没有区别</main>    <section>内容区域,和main没有区别</sect

    <nav>导航标签 和div标签一样,块属性标签</nav>     <main>内容区域,和section没有区别</main>     <se ...

  2. iPhoneX适配问题 iOS刘海屏 安全区域处理 IOS小黑条处理 IOS兼容处理

    IphoneX适配问题 iOS刘海屏 安全区域处理 前言 适配前需要了解的几个关键字 安全区域 viewport-fit env() 和 constant() 适配的方法 第一步 设置网页在可视区的布 ...

  3. Python使用matplotlib函数subplot可视化多个不同颜色的折线图、为多个子图添加总标题(main title)

    Python使用matplotlib函数subplot可视化多个不同颜色的折线图.为多个子图添加总标题(main title) 目录

  4. Python使用matplotlib函数subplot可视化多个不同颜色的折线图、为多个子图添加总标题(main title)、自定义设置主标题字体类型、字体大小、字体颜色等

    Python使用matplotlib函数subplot可视化多个不同颜色的折线图.为多个子图添加总标题(main title).自定义设置主标题字体类型.字体大小.字体颜色等 目录

  5. python使用matplotlib可视化subplots子图、为subplots添加主标题(main title)、为subplots的每一个子图添加子图标题(subplots title)

    python使用matplotlib可视化subplots子图.为subplots添加主标题(main title).为subplots的每一个子图添加子图标题(subplots title) 目录

  6. 2个网页跳来跳去_怎么写最优化的网页标题标签(Title Tag)?

    通过简单的观察就可以发现: 排在搜索引擎搜索结果前几页的网页或网站中,一般是这个网页或网站的标题标签(Title Tag)中包含着搜索关键词.所以,反过来,要让自己网页或网站排到搜索结果的前列,网页的 ...

  7. iOS汉语转拼音以及按照拼音排序

    在iOS中汉语转拼音可以使用系统提供的方法,也可以使用第三方库来实现,但是网上说使用系统方法实现的话性能会比较差,此处只是提供方法,至于选择使用哪种方法,自己看情况而定. 1.系统方法 主要使用到了方 ...

  8. iOS--中文排序、全拼音排序

    本文示例Demo在GitHub:https://github.com/gaussli/ChineseSortDemo 在做项目的时候,总遇到排序问题,英文排序是很简单的,直接使用compare方法就可 ...

  9. IOS 汉字拼音排序

    在网上找了好久,没有找到IOS版拼音排序的好例子,在项目中用到了排序,总结出来之后,写了一个小例子供大家参考 -(NSMutableArray*) sortByPinyin:(NSMutableArr ...

最新文章

  1. linux看测试环境版本,最新版 EOS 在 Ubuntu 18.04.1 LTS 环境上的安装测试教程
  2. 路径字符串生成树形结构的思路_LeetCode 22. 括号生成
  3. c语言u64数据类型打印,小谈C语言中常见数据类型在32及64位机上的使用
  4. python自动化办公都能做什么菜-Python自动化开发学习之三级菜单制作
  5. mapreduce编程实例(1)-统计词频
  6. JVM实战与原理---Class文件结构
  7. SDWebImage详解
  8. js如何判断一个对象是不是Array
  9. c++ opencv编程实现暗通道图像去雾算法_OpenCV图像处理专栏十三 | 利用多尺度融合提升图像细节...
  10. 【web开发】☆★之利用POI操作Excel表格系列教程【6】遍历工作簙行和列取值
  11. Keil uVision4 安装包及破解程序
  12. 树莓派设置开机自启动程序
  13. 破局模块总结 -- 宁向东的清华管理学课总结
  14. 12306网站服务器时间限制,12306网上订票时间限制
  15. 微信支付分 - 完结支付分订单API
  16. hdu5594 ZYB's Prime
  17. FL Studio音乐编曲入门教程
  18. Broadcast Receiver @
  19. PHP-课程设计-LOL英雄资料网站
  20. 入门知识(二)WPF所有的画刷工具

热门文章

  1. 无法安装Windows Live“OnCatalogResult:0x80190194”错误的解决方法
  2. Python xlwt : More than 4094 XFs (styles) 解决方法
  3. 从天地图下载瓦片构建本地瓦片地图
  4. MySQL性能优化之道
  5. git push 使用总结
  6. 研究人员发现绝大部分酷派(Coolpad)手机暗藏后门(转)
  7. request.getParameterValues与request.getParameter的区别
  8. 软件测试中有关界面测试经验总结
  9. VS2015解决非Unicode编码包含中文字段无法编译的问题
  10. Several ports (8080, 8009) required by Tomcat v9.0 Server at localhost are already in use.解决方案