iOS 使用UILocalizedIndexedCollation实现区域索引标题(Section Indexed Title)即拼音排序
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 = ; 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
iOS 使用UILocalizedIndexedCollation实现区域索引标题(Section Indexed Title)即拼音排序的更多相关文章
- 使用UILocalizedIndexedCollation实现区域索引排序 及 不显示没有数据的区域
UILocalizedIndexedCollation可以实现区域排序,类似通讯录的样式. //首先进行初始化 locationCollation = [UILocalizedIndexedColla ...
- ios 汉字字符串数组拼音排序
ios没有提供简单的汉字拼音排序方法,在网上看到了oc方法,这里写以下对应的swift方法 var stringCompareBlock: (String,String)->Bool = { ( ...
- [题解]NOIP2018(普及组)T1标题统计(title)
NOIP2018(普及组)T1标题统计(title) 题解 [代码(AC)] #include <iostream> #include <cstdio> #include &l ...
- 掘金 里面 写文章 带目录的时候 用#(空格)标题 后面用## title,一个页面只有一个H1
掘金 里面 写文章 带目录的时候 用#(空格)标题 后面用## title,一个页面只有一个H1
- 【IOS】模仿windowsphone列表索引控件YFMetroListBox
有没有觉得UITableView自带的右侧索引很难用,我一直觉得WindowsPhone中的列表索引非常好用. 所以呢,我们来实现类似Windows Phone中的列表索引(这就是信仰). 最终实现效 ...
- IOS之表视图添加索引
我们要实现的效果如下. 1.修改ControlView.h,即添加变量dict,用于存储TabelView的数据源. #import <UIKit/UIKit.h> @interface ...
- iOS开发(Objective-C)常用库索引
code4app.com 这网站不错,收集各种 iOS App 开发可以用到的代码示例 cocoacontrols.com/ 英文版本的lib收集 objclibs.com/ 精品lib的收集网站 h ...
- [iOS微博项目 - 1.5] - NavigationBar标题按钮
A.NavigationBar标题按钮 1.需求 在“首页”的导航栏中部设置一个“首页”文字+箭头按钮 统一设置样式 根据实际文本长度调整宽度 消除系统自带的点击高亮效果 点击按钮,箭头上下颠倒 gi ...
- iOS之UIWebView无法获取web标题
最近遇到了一个问题,就是在UIWebView的代理方法里,执行document.title的js代码无法获取网页标题,代码如下: - (void)webViewDidFinishLoad:(UIWeb ...
随机推荐
- hdu1728逃离迷宫 (利用最短路径思想+优先队列(BFS))
Problem Description 给定一个m × n (m行, n列)的迷宫,迷宫中有两个位置,gloria想从迷宫的一个位置走到另外一个位置,当然迷宫中有些地方是空地,gloria可以穿越,有 ...
- javascript高级知识分析——灵活的参数
代码信息来自于http://ejohn.org/apps/learn/. 使用数量可变的参数对编程很有好处 function merge(root){ for(i = 0 ; i < argum ...
- CRM Entity 之Money转string int类型等
Money转string 左右都是string //服务站地址 vehicleDetail["yt_servicestation_address"]=serviceStationC ...
- C# - 动态连接数据库字符串
String conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|数据库文件.mdf;Integrated ...
- QF——iOS第三方登录和社会化分享
QQ登录的流程: 1.下载SDK,并添加到项目中: 2.添加SDK需要的依赖库,以及配置文件: 3.重写APPDelegate的方法handleOpenURL和openURL: 4.实现Tencent ...
- jQuery1.9.1针对checkbox的调整
在jquery 1.8.x中的版本,我们对于checkbox的选中与不选中操作如下: 判断是否选中 $('#checkbox').prop('checked') 设置选中与不选中状态: $('#che ...
- 安装虚拟机VMWare时出现1021错误的解决办法
今天安装虚拟机(VMWare Workstation9.0),中途老是出现错误:Failed to create the requested registry key key installer er ...
- PHP fopen和fwrite函数实现创建html页面
思路 用fopen函数和fread函数得到模板,然后用str_replace函数替换模板标签为变量,最后用fwrite函数输出新的HTML页面 index.html模板页面 <!DOCTYPE ...
- werkzeug源码阅读笔记(二) 下
wsgi.py----第二部分 pop_path_info()函数 先测试一下这个函数的作用: >>> from werkzeug.wsgi import pop_path_info ...
- 什么是JSON对象
1.什么是json? JSON全称是JavaScript Object Notation,是一种轻量级的数据交换格式.JSON 与XML具有相同的特性,是一种数据存储格式,但是JSON相比XML 更易 ...