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)即拼音排序的更多相关文章

  1. 使用UILocalizedIndexedCollation实现区域索引排序 及 不显示没有数据的区域

    UILocalizedIndexedCollation可以实现区域排序,类似通讯录的样式. //首先进行初始化 locationCollation = [UILocalizedIndexedColla ...

  2. ios 汉字字符串数组拼音排序

    ios没有提供简单的汉字拼音排序方法,在网上看到了oc方法,这里写以下对应的swift方法 var stringCompareBlock: (String,String)->Bool = { ( ...

  3. [题解]NOIP2018(普及组)T1标题统计(title)

    NOIP2018(普及组)T1标题统计(title) 题解 [代码(AC)] #include <iostream> #include <cstdio> #include &l ...

  4. 掘金 里面 写文章 带目录的时候 用#(空格)标题 后面用## title,一个页面只有一个H1

    掘金 里面 写文章 带目录的时候 用#(空格)标题 后面用## title,一个页面只有一个H1

  5. 【IOS】模仿windowsphone列表索引控件YFMetroListBox

    有没有觉得UITableView自带的右侧索引很难用,我一直觉得WindowsPhone中的列表索引非常好用. 所以呢,我们来实现类似Windows Phone中的列表索引(这就是信仰). 最终实现效 ...

  6. IOS之表视图添加索引

    我们要实现的效果如下. 1.修改ControlView.h,即添加变量dict,用于存储TabelView的数据源. #import <UIKit/UIKit.h> @interface  ...

  7. iOS开发(Objective-C)常用库索引

    code4app.com 这网站不错,收集各种 iOS App 开发可以用到的代码示例 cocoacontrols.com/ 英文版本的lib收集 objclibs.com/ 精品lib的收集网站 h ...

  8. [iOS微博项目 - 1.5] - NavigationBar标题按钮

    A.NavigationBar标题按钮 1.需求 在“首页”的导航栏中部设置一个“首页”文字+箭头按钮 统一设置样式 根据实际文本长度调整宽度 消除系统自带的点击高亮效果 点击按钮,箭头上下颠倒 gi ...

  9. iOS之UIWebView无法获取web标题

    最近遇到了一个问题,就是在UIWebView的代理方法里,执行document.title的js代码无法获取网页标题,代码如下: - (void)webViewDidFinishLoad:(UIWeb ...

随机推荐

  1. 触发按钮改变panel面板上的小圆圈颜色

    import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TouChaCol implements ...

  2. struts 2 三目运算

    <s:set id="styleList" value="{'default','success','warning'}"/> <s:iter ...

  3. ME525+ 刷机工具及设置中心号码

    接上篇: 刷机包下载地址http://sbf.droid-developers.org/umts_jordanplus/list.php,选择一款大陆包.... 设置中心号码: 拨打   *#*#46 ...

  4. linux学习之(六)-主机名、网络IP的配置与查看

    设置Linux 本机IP有一个非常好用的命令就是setup命令,在Linux终端打入setup命令就会打开Linux配置窗口,如下图: . 在打开的窗口中通过上下键选择  Network config ...

  5. #include <fstream>

    1 fstream 2 ifstream 3 ofstream 4 seekg 5 seekp 6 tellg 7 tellp 1 fstream 打开输入输出文件流 #include <ios ...

  6. html表格单元格设置背景颜色

  7. javascript高级知识分析——实例化

    代码信息来自于http://ejohn.org/apps/learn/. new做了什么? function Ninja(){ this.name = "Ninja"; } var ...

  8. window.onload()与$(document).ready()区别

    浏览器加载完DOM后,会通过javascript为DOM元素添加事件,在javascript中,通常使用window.onload()方法. 在jquery中,则使用$(document).ready ...

  9. Linux 环境变量和source命令 (转)

    可能是班门弄斧了,仅share给尚不知道的童鞋. 1.       问题的来源: 为什么我们编译Android代码时,需要输入:  source ./build/envsetup.sh  或者 . . ...

  10. 场景切换特效Transition——Cocos2d-x学习历程(十二)

    Transition 场景切换 在游戏中通常会用到一些场景的切换,比如从加载界面切换到欢迎界面.游戏中的所有场景存放在一个栈中,有且只有一个场景可以处于激活状态.直接replaceScene(即不适用 ...