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. 13、NFC技术:读写非NDEF格式的数据

    MifareUltralight数据格式 将NFC标签的存储区域分为16个页,每一个页可以存储4个字节,一个可存储64个字节(512位).页码从0开始(0至15).前4页(0至3)存储了NFC标签相关 ...

  2. PDF数据提取------2.相关类介绍

    1.简介 构造数据类型PdfString封装Rect类,PdfAnalyzer类中定义一些PDF解析方法. 2.PdfString类与Rect类 public class PdfString : IC ...

  3. jQuery中的bind绑定事件与文本框改变事件的临时解决方法

    暂时没有想到什么好的解决办法,我现在加了个浏览器判断非ie的话就注册blur事件,这样有个问题就是blur实在别的控件活动焦点的时候,txtStation控件注册的方法是为了填充它紧挨着的一个下拉列表 ...

  4. mongdb创建自增主键(primary key)的相关讨论 - Jason.Zhi

    根据mongodb官方文档介绍,如果在插入(insert)操作时,没有指定主键id,那么它会自动给插入行自动附上一个主键id.看起来不错,但是详细看看,就会发现这个id值有点复杂. 如下图: mong ...

  5. MoveTo和MoveBy

    cc.MoveTo是“移动到这里",而cc.MoveBy则是“相对于之前点再移动”,通俗一点就是说这里需要两个坐标pos1(x1,y1),pos2(x2,y2). 如果是cc.MoveTo的 ...

  6. Xtrabackup之innobackupex备份恢复详解(转)

    add by zhj:对于Xtrabackup2.2来说,已经解决了本文结尾提到的那个bug,当使用--copy-back时,同时加--force-non-empty-directories 即可.这 ...

  7. ms-class的进化

    ms-class是avalon用得最多的几个绑定之一,也正因为如此其功能一直在扩充中.根据时期的不同,分为旧风格与新风格两种. 旧风格是指正在ms-class后面跟着类外,然后在绑定值中添加表达式,即 ...

  8. 适应所有浏览器的cookie

    //设置cookie的方法 weiyingfunction SetCookie(a, b) {        var d = new Date();    var v = arguments;    ...

  9. Hibernate资源

    正在学马士兵Hibernate的同学来看这里,这里提供了他视频里需要的JAR包,请尽情下载,给好评喔. 一.Hibernate 3.3.2 核心JAR包 http://pan.baidu.com/s/ ...

  10. codeforces 601A The Two Routes(最短路 flody)

    A. The Two Routes time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...