使用UILocalizedIndexedCollation实现区域索引排序 及 不显示没有数据的区域
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实现区域索引排序 及 不显示没有数据的区域的更多相关文章
- 一条Sql语句分组排序并且限制显示的数据条数
如果我想得到这样一个结果集:分组排序,并且每组限定记录集的数量,用一条SQL语句能办到吗? 比如说,我想找出学生期末考试中,每科的前3名,并按成绩排序,只用一条SQL语句,该怎么写? 表[TScore ...
- iOS 使用UILocalizedIndexedCollation实现区域索引标题(Section Indexed Title)即拼音排序
UITableView在行数相当多的时候,给人的感觉是非常笨重的.通常为了方便用户使用,采用的方法有:搜索框.按层级展示.区域索引标题. 前两种就不用介绍了,此文就介绍区域索引标题的实现. 区域索引标 ...
- IOS UITableView索引排序功能
UITbableView分组展示信息时,有时在右侧会带索引,右侧的索引一般为分组的首字母,比如城市列表的展示.当点击右侧索引的字母,列表会快速跳到索引对应的分组,方便我们快速查找.下面,就介绍一下索引 ...
- Null 值对索引排序的影响案例一则
--原SQL 语句如下:select * from (select tmp_tb.*, ROWNUM row_id from (select wpid, customer_id, customer_n ...
- C# 指定索引排序 (原)
private void test(string[] sortkey_list, string[] list_temp) { //打开excel到dt: " }; string[] roww ...
- Smart3D系列教程7之 《手动配置S3C索引加载全部的瓦片数据》
一.前言 迄今为止,Wish3D已经出品推出了6篇系列教程,从倾斜摄影的原理方法.采集照片的技巧.Smart3D各模块的功能应用.小物件的照片重建.大区域的地形重建到DSM及正射影像的处理生产,立足于 ...
- Mysql高级操作学习笔记:索引结构、树的区别、索引优缺点、创建索引原则(我们对哪种数据创建索引)、索引分类、Sql性能分析、索引使用、索引失效、索引设计原则
Mysql高级操作 索引概述: 索引是高效获取数据的数据结构 索引结构: B+Tree() Hash(不支持范围查询,精准匹配效率极高) 树的区别: 二叉树:可能产生不平衡,顺序数据可能会出现链表结构 ...
- 终于懂了:FWinControls子控件的显示是由Windows来管理,而不是由Delphi来管理(显示透明会导致计算无效区域的方式有所不同——透明的话应减少剪裁区域,所以要进行仔细计算)
在研究TCustomControl的显示过程中,怎么样都找不到刷新FWinControls并重新显示的代码: procedure TWinControl.PaintHandler(var Messag ...
- js 判断一个元素是否在滚动的可视区域内,不在就固定到可视区域的上方。
前言:最近工作中,有这样一个场景,判断一个元素是否在滚动的可视区域内,不在就固定到可视区域的上方.为了以后再次遇到,所以记录下来,并分享.转载请注明出处:https://www.cnblogs.com ...
随机推荐
- php.ini中Magic_Quotes_Gpc开关设置
如果你网站空间的php.ini文件里的magic_quotes_gpc设成了off,那么PHP就不会在敏感字符前加上反斜杠(\\),由于表单提交的内容可能含有敏感字符,如单引号('),就导致了SQL ...
- 嵌入式 VFS: Cannot open root device "mtdblock2" or unknown-block(2,0)
系统启动后,虽然nand驱动表现正常,但是最后挂载rootfs时候出错: Kernel command line: root=/dev/mtdblock2 rw init=/linuxrc conso ...
- A woman without arms
任吉美出生在中国烟台海阳一个极为普通的渔民家里.她先天残疾,没有胳膊和手. 小吉美注定要比别人生活得更艰难.她不能自己穿衣,不能自己端碗吃饭,也不能像兄弟姐妹们一样帮助妈妈干家务活,她觉得自己成了家里 ...
- ubuntu 下安装sh 文件
1. cd 到 指定文件夹 如: cd /home/ddy/下载 2. sudo chmod +x *.sh 3. sudo ./*.sh ok 了 (1)数据预处理 可以用下载好的数据集,也可 ...
- Linux学习--第二波
虽然安装的centos感觉不能上网,权限也不知道怎么设置. 偶然的机会发现了一个好东西,博客:http://www.cnblogs.com/xiaoluo501395377/tag/CentOS/.有 ...
- 【boost】BOOST_LOCAL_FUNCTION体验
c++11里支持使用lambda在函数内定义本地嵌套函数,将一些算法的判断式定义为本地函数可以使代码更加清晰,同时声明和调用靠近也使得更容易维护.遗憾的是公司开发平台任然停留在vs2008,使用boo ...
- Hbase学习记录(1)|伪分布式安装
概述 Hbase –Haddop Database 是一个高性能,高可靠性.面向列.可伸缩的分布式存储系统. Hbase利用HDFS作为文件存储系统,利用MapReduce来处理Hbase的海量数据, ...
- share-jquery
html val text区别: .html()用为读取和修改元素的HTML标签 .text()用来读取或修改元素的纯文本内容 .val()用来读取或修改表单元素的value值. 这三个方法功能上的对 ...
- 第二百二十二天 how can I 坚持
纪念碑谷好费脑子啊,头都大了,被遗忘的海最后百度了下攻略才过了. 今天下班遇到了易军,哎,总感觉怪怪的,心情顿时压抑了些,源二生日,一起去吃了个饭,烤鸭,吃的挺不错. 创新去哪了,其实每个人的内心深处 ...
- 如何使cron任务出错时发邮件
如果设置了 MAILTO,cron就会将任务的标准输出和标准错误输出重定向到邮箱(即发送邮件).但如果只想接到错误报警邮件 -- 即任务正常执行时不发送,只在出错时发送 -- 应该怎么实现呢? 方法很 ...