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. mysql 查询随机条记录的sql语句和php计算概率

    最近在网上找了下mysql查询随机的几个sql,我把最终的记录下来. SELECT * FROM uchome_mtag AS a JOIN (SELECT MAX(tagid) AS id FROM ...

  2. ubuntu 上更新安装 openoffice.org3的过程

    方法一:手动安装1首先在“应用成程序--添加/删除”里卸载openoffice 2.4,可能openoffice.org 2.4 Draw因为关联而无法卸载,忽略,卸载其他几项.然后在中文官方网上下载 ...

  3. 1.解剖Linq to object

    LINQ想必大家都不陌生了,它的出现使得我们的代码变得更短.更优雅了.至于LINQ是什么,Linq to object这类的扩展方法到底做了些什么.我们使用的EF是如何实现的(如何解析Expressi ...

  4. poj 3270(置换群)

    题意:给定n头母牛的脾气大小,然后让你通过交换任意两头母牛的位置使得最后的母牛序列的脾气值从小到大,交换两头母牛的代价是两个脾气之和,使得代价最小. 分析:以前做过一道题,只有一个地方和这道题不同,但 ...

  5. 《Python 学习手册4th》 第十九章 函数的高级话题

    ''' 时间: 9月5日 - 9月30日 要求: 1. 书本内容总结归纳,整理在博客园笔记上传 2. 完成所有课后习题 注:“#” 后加的是备注内容 (每天看42页内容,可以保证月底看完此书) “重点 ...

  6. C++ 中类的构造函数理解(二)

    C++ 中类的构造函数理解(二) 写在前面 上次的笔记中简要的探索了一下C++中类的构造函数的一些特性,这篇笔记将做进一步的探索.主要是复制构造函数的使用. 复制构造函数 复制构造函数也称拷贝构造函数 ...

  7. linux笔记_20150825_linux有什么好处

    那么多人在用,linux到底有毛好处? 其实我也不太清楚,有人说免费,可是大家用windows也不要钱的.我想在天朝,要钱的软件不多吧.一个子也不用花.真心感谢为人民服务的那些大牛. 现在,除了在ub ...

  8. 【转】requirejs简单入门

    博主今天正式工作啦,工作中用到了js模块化技术,这里转来一个入门教程,很易懂,转给同样刚入门的你们~~ 原地址:http://www.ruanyifeng.com/blog/2012/11/requi ...

  9. MarkdownPad 破解学习

    最近学习 Markdown,从网上下载了 Windows 下的编辑器:MarkdownPad.这款软件分为免费版和专业版(收费), 对于普通用户来说免费版已经足够,专业版比免费版多了如下几个功能: 一 ...

  10. struts2传递List对象(复合对象)

    1.前台jsp界面: <%@ page language="java" contentType="text/html; charset=utf-8" pa ...