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 ...
随机推荐
- mysql允许远程IP访问
默认情况下Linux内的mysql数据库mysql,user表内的用户权限只是对localhost即本机才能登陆.需要更改权限: mysql> Grant all privileges on * ...
- 帮助中心 7D-我的私家设计师 设计师品牌服饰集成网 7D服装定制!
帮助中心 7D-我的私家设计师 设计师品牌服饰集成网 7D服装定制! 关于我们
- Log4Net不生成日志文件
可能没有初始化配置,在Global文件Application_Start添加 log4net.Config.XmlConfigurator.Configure(); 或者输出日志进行初始化,如(Log ...
- Cube(规律)
Cube Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- UI标签库专题十三:JEECG智能开发平台 ckfinder(ckfinder插件标签)
1. ckfinder(ckfinder插件标签) 1.1. 參数 属性名 类型 描写叙述 是否必须 默认值 name string 属性名称 是 null value string 默认值 否 nu ...
- DOM 样式操作
通过js动态的修改样式 更新样式的方法:一.使用.style方法修改样式,缺点是使样式混杂在js中,再次修改不易.二.更新class属性,更改样式.三.一次性更改很多元素样式(如换肤操作),更改样式表 ...
- Android源码学习(一) 数据集观察者
查看Android源码发现这个,决定记下下来. 1.在android.database这个包下面,存在这样一个抽象类DataSetObserver,里面包括onChanged()和onInvalida ...
- sql server存储过程分页
Create PROCEDURE [dbo].[Table_GetList] ) = '', -- 查询条件(注意: 不要加 WHERE) ) = '', -- 设置排序 , -- 页尺寸 , -- ...
- AngularJs 实例
1.AngularJs 表单验证: 示例 .controller('signupController', ['$scope', function($scope) { $scope.submitted ...
- vb.net常用函数
当然,这些都可以从MSDN查到,但是有时候打开帮助老慢的,所以先放到这里放一放,查个函数什么的比较快一点.都是从网上搜来的.Abs(number) 取得数值的绝对值. Asc(String) 取得字符 ...