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 ...
随机推荐
- poj3650---将一个字符串中的特定字符转换
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { ]; int i; w ...
- Android UI ActionBar功能-ActionBarSherlock 的使用
ActionBarSherlock实现了在ActionBar上添加一个下拉菜单的功能,也是App常用的功能之一: ActionBarSherlock是第三方提供的一个开源类库,下载地址:http:// ...
- Linux mint 17中文输入法安装,改动linux mint与windows7双系统启动顺序
安装好linux mint17后,进入mint系统,首先须要一个比較合适的中文输入法. 一.首先迎来的就是安装中文输入法了,之前听说搜狗为ubuntu kinly定制了输入法,所以就想安装搜狗输入法, ...
- SSIS之数据转换用法
当SSIS报错为:“无法在unicode和非unicode字符串数据类型之间转换”,可以考虑用数据转换器实现,很简单,如下图: 第一步,找到数据转换器: 第二步,编辑数据转换器: 第三步:编辑目标映射 ...
- Android入门——UI(9)
SwipRefreshLayout下拉刷新控件 <?xml version="1.0" encoding="utf-8"?> <android ...
- 教你如何利用初级C#语言更改银行存款!!!!
您是否对生活现状不满意? 您是否总是感叹工资太少? 您是否经常发现自己相中的物品又降价了然而自己却还是只能望洋兴叹? 没关系!让我来拯救你的钱包! 接下来进入正题: 要想更改自己的银行存款首先得找到一 ...
- EJB QI查询
EO.PNAME like ?1 OR EO.PNAME like ?2"; Query query = entityManager.createNativeQuery(quer ...
- EC读书笔记系列之12:条款22、23、24
条款22 将成员变量声明为private 记住: ★切记将成员变量声明为private.这可赋予客户访问数据的一致性.可细微划分访问控制.允诺约束条件获得保证,并提供class作者以充分的实现弹性. ...
- puts fputs printf的区别
puts()显示字符串时自动在其后添加一个换行符,函数里的参数是一个地址,从该地址向后面输出,直到遇到空字符,所以要确保输出的字符串里要有空字符.与gets()函数一起使用. fputs()需要第二个 ...
- 周末苦逼码代码,为css3的强大所颤抖了
周末小哥我看完了几个星期没追的行尸走肉和生活大爆炸(感谢大A站!),感觉生活真的好空虚,没想到我现在居然会对游戏失去了兴趣!!!代码的魔力真的是无法用语言形容...(我真假...)百无聊赖,在电脑上装 ...