UILocalizedIndexedCollation可以实现区域排序,类似通讯录的样式。

//首先进行初始化

locationCollation = [UILocalizedIndexedCollation currentCollation];

//获取section标题 A-Z#

array_collation_title =[[NSMutableArray alloc]initWithArray:[locationCollation sectionTitles]];

  //构建每个section数组
array_section =[[NSMutableArray alloc]init];
for (int i=; i<array_collation_title.count; i++) {
NSMutableArray *subArray = [[NSMutableArray alloc]init];
[array_section addObject:subArray];
}
//排序
//排序对象,放进分区数组中
for (Person *person in array_datas) {
NSInteger section = [locationCollation sectionForObject:person collationStringSelector:@selector(name)];
NSMutableArray *subarray =array_section[section];
[subarray addObject:person];
} //分别对分区进行排序
for (int i =;i<array_section.count;i++) {
NSMutableArray *subarray = [array_section objectAtIndex:i];
NSArray *sortArr = [locationCollation sortedArrayFromArray:subarray collationStringSelector:@selector(name)];
[array_section replaceObjectAtIndex:i withObject:sortArr];
}   
#pragma mark -datasouth
//返回section 要显示的标题集合
-(NSArray<NSString *>*)sectionIndexTitlesForTableView:(UITableView *)tableView{
// return [[UILocalizedIndexedCollation currentCollation]sectionTitles];
return array_collation_title;
} //根据section 返回显示的标题
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
// return [[[UILocalizedIndexedCollation currentCollation]sectionTitles]objectAtIndex:section];
return [array_collation_title objectAtIndex:section];
} //点击右侧字母,触发此方法,告诉数据源 选中的section
-(NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index{
return [[UILocalizedIndexedCollation currentCollation]sectionForSectionIndexTitleAtIndex:index];
} -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
int count = ;
for (NSMutableArray *subArr in array_section) {
if (subArr.count>) {
count++;
}
}
return count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSMutableArray *subArr = [array_section objectAtIndex:section];
return subArr.count;
} -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
if (cell==nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
NSMutableArray *subArray = [array_section objectAtIndex:indexPath.section];
Person *person =[subArray objectAtIndex:indexPath.row];
if (person!=nil) {
[cell.textLabel setText:person.name];
} return cell;
} -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"section = %d,row = %d",indexPath.section,indexPath.row);
} -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
return ;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return ;
}

 

运行效果图

运行后发现 在 没有值的section下 依然显示section标题,这样会感觉非常丑。

优化一下代码,

添加以下方法

 //删除没有值的subArr
for (int i =;i<array_section.count;i++) {
NSMutableArray *subarray = [array_section objectAtIndex:i];
if (subarray.count==) {
[array_section removeObjectAtIndex:i];
[array_collation_title removeObjectAtIndex:i];
i--; // 这里很重要
}
}

使用UILocalizedIndexedCollation实现区域索引排序 及 不显示没有数据的区域的更多相关文章

  1. 一条Sql语句分组排序并且限制显示的数据条数

    如果我想得到这样一个结果集:分组排序,并且每组限定记录集的数量,用一条SQL语句能办到吗? 比如说,我想找出学生期末考试中,每科的前3名,并按成绩排序,只用一条SQL语句,该怎么写? 表[TScore ...

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

    UITableView在行数相当多的时候,给人的感觉是非常笨重的.通常为了方便用户使用,采用的方法有:搜索框.按层级展示.区域索引标题. 前两种就不用介绍了,此文就介绍区域索引标题的实现. 区域索引标 ...

  3. IOS UITableView索引排序功能

    UITbableView分组展示信息时,有时在右侧会带索引,右侧的索引一般为分组的首字母,比如城市列表的展示.当点击右侧索引的字母,列表会快速跳到索引对应的分组,方便我们快速查找.下面,就介绍一下索引 ...

  4. Null 值对索引排序的影响案例一则

    --原SQL 语句如下:select * from (select tmp_tb.*, ROWNUM row_id from (select wpid, customer_id, customer_n ...

  5. C# 指定索引排序 (原)

    private void test(string[] sortkey_list, string[] list_temp) { //打开excel到dt: " }; string[] roww ...

  6. Smart3D系列教程7之 《手动配置S3C索引加载全部的瓦片数据》

    一.前言 迄今为止,Wish3D已经出品推出了6篇系列教程,从倾斜摄影的原理方法.采集照片的技巧.Smart3D各模块的功能应用.小物件的照片重建.大区域的地形重建到DSM及正射影像的处理生产,立足于 ...

  7. Mysql高级操作学习笔记:索引结构、树的区别、索引优缺点、创建索引原则(我们对哪种数据创建索引)、索引分类、Sql性能分析、索引使用、索引失效、索引设计原则

    Mysql高级操作 索引概述: 索引是高效获取数据的数据结构 索引结构: B+Tree() Hash(不支持范围查询,精准匹配效率极高) 树的区别: 二叉树:可能产生不平衡,顺序数据可能会出现链表结构 ...

  8. 终于懂了:FWinControls子控件的显示是由Windows来管理,而不是由Delphi来管理(显示透明会导致计算无效区域的方式有所不同——透明的话应减少剪裁区域,所以要进行仔细计算)

    在研究TCustomControl的显示过程中,怎么样都找不到刷新FWinControls并重新显示的代码: procedure TWinControl.PaintHandler(var Messag ...

  9. js 判断一个元素是否在滚动的可视区域内,不在就固定到可视区域的上方。

    前言:最近工作中,有这样一个场景,判断一个元素是否在滚动的可视区域内,不在就固定到可视区域的上方.为了以后再次遇到,所以记录下来,并分享.转载请注明出处:https://www.cnblogs.com ...

随机推荐

  1. 使用Jquery Mobile设计Android通讯录

    本系列教程将指导大家一步步使用Jquery Mobile设计一个Android的通讯录应用.其中在应用的界面部分,将使用jQuery Mobile框架,并且会指导大家如何使Android中提供的web ...

  2. YII Framework学习教程-YII的日志

    日志的作用(此处省略1000字) YII中的日志很好很强大,允许你把日志信息存放到数据库,发送到制定email,存放咋文件中,意见显示页面是,甚至可以用来做性能分析. YII中日志的基本配置:/yii ...

  3. 实测 windows下nginx很不稳定,换成squid好多了

    在windows server 2003 下尝试了号称支持50,000个并发连接数响应的nginx,结果相当不理想. 具体表现为时不时网页就卡住不动了,需要再刷新一次网页才能显示,而服务器cpu和内存 ...

  4. FreeMarker笔记 第二章 数值和类型

    2.1 基本内容 2.1.1 简介 2.1.2 什么是数值 和程序语言中的数值类型是相似的. 2.1.3 什么是类型? 2.1.4 数据模型是哈希表 2.2 类型 2.2.1 简介 2.2.2 标量 ...

  5. PermGen space Eclipse 终极解决方案

    1.选中项目右键 run or debug configurations... 2.在 VM arguments 加入  -Xms128m -Xmx512m -XX:PermSize=64M -XX: ...

  6. 用Python组合Celery Redis RabbitMQ进行分布式数据抓取

    首先,记录下遇到的问题吧,在抓取的过程中为了避免IO操作,主要用Redis做插入缓存,当内存占用率很大时,会周期性的持续到Mysql里 虽然是拆东墙补西墙,但把数据抓取完毕后持续化可以慢慢进行,毕竟数 ...

  7. Scrum概述

    • 敏捷方法是一类软件开发流程的泛称: • 敏捷方法是相对于传统的瀑布式软件过程提出的: • 敏捷方法可以用敏捷宣言(4条).敏捷原则(12条)来概括: • 敏捷原则通过一系列的敏捷实践来体现出来: ...

  8. asp.net mvc下文件上传

    典型的文件上传表单 <form action="/File" enctype="multipart/form-data" method="pos ...

  9. poj 2196 Specialized Four-Digit Numbers

    如果一个数字 十进制的各位数的和 == 十六进制的各位数的和 == 十二进制的各位数的和,则输出,从2992到9999 #include <cstdio> int toDD(int n) ...

  10. php 的简单易用的调式方法,打印方法

    简单的调试方法:echo, print_r, var_dump, exit, debug_backtrace(), debug_print_backtrace(), gettype(), get_cl ...