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. 【Android】不弹root请求框检测手机是否root

    由于项目需要root安装软件,并且希望在合适的时候引导用户去开启root安装,故需要检测手机是否root. 最基本的判断如下,直接运行一个底层命令.(参考https://github.com/Trin ...

  2. js中关于arguments

  3. 左侧菜单 z

    Dev 的tabControl

  4. LoadRunner界面分析(二)

    1.Controller 2.创建运行场景 3.方案设计 4.Resuls settting 5.监视方案

  5. Ubuntu 16.04 TensorFlow CPU 版本安装

    1.下载Anaconda,官方网站.我下载的时Python 2.7 64bit版本: 2.安装执行命令     bash Anaconda2-4.2.0-Linux-x86_64.sh 设置好目录后等 ...

  6. MATLAB 通过二进制读写文件

    这几天在做信息隐藏方面的应用,在读写文本文件时耗费许久,故特别的上网学习一二,这里给出一常用读写,其他的都类似. 很多时候,我们都要将一个.txt以二进制方式读出来,操作后在恢复成.txt文本. ma ...

  7. Leetcode: Length of Last Word in python

    Length of Last Word Total Accepted: 47690 Total Submissions: 168587     Given a string s consists of ...

  8. Github在windows7环境下使用入门

    1.下载并安装 下载和安装一般都没什么问题,网上的链接一大堆,不过还是在此给一个安装的地址和安装的参考吧. 当然,安装完成后要保证git能使用,必须配置github 2.配置github 首先是要创建 ...

  9. Hadoop概述

    本章内容 什么是Hadoop Hadoop项目及其结构 Hadoop的体系结构 Hadoop与分布式开发 Hadoop计算模型—MapReduce Hadoop的数据管理 小结 1.1 什么是Hado ...

  10. gem 相关命令

    gem #查看gem源 gem sources # 删除默认的gem源 gem sources --remove http://rubygems.org/ # 增加taobao作为gem源 gem s ...