我最近工作中的一个iOS App中经常有在不同的场合,隐现菜单列表里某一项的需求.
如果初始化的时候就去掉某一项的话,有可能让序号变化, 处理上会比较麻烦容易出错.
我采用了初始化列表相同但是隐藏section的方式,保持序号不变,方便处理.
那么如何隐藏一个section呢?
其实很简单,就是将section的高度设置为0
重载 heightForRowAtIndexPath方法, 假设要隐藏section 1的话,
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if(indexPath.section == 1)
return 0;
else
return 60;
}
简单的列表这样就可以了.
如果你的菜单项带有header和footer, 也需要将他们隐藏, 同理重载heightForHeaderInSection 和 heightForFooterInSection 方法

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
if(section == 1)
return 0.01;
else
return 18;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
if(section == 1)
return 0.01;
else
return 16;
}

注意: 这里没有return 0, 而是return 0.01, 是因为这两个方法不接受0值, 返回0会不起作用. 因为是返回float类型, 所以返回一个较小的数,比如0.01之类的.
另外.如果你派生了viewForFooterInSection 或 viewForHeaderInSection, 隐藏的section要返回nil,否则隐藏不了, 这一点也很重要.
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
if (section == 0) {
return footView0;
} else if (section == 1) {
return nil;
} else if (section == 2) {
return footView2;
} else if (section == 3) {
return footView3
} else if (section == 4)
return footView4;
return nil;
}

viewForHeaderInSection也是同理, 这样就可以隐藏一个section及其header和footer了

如何隐藏UITableView中的一项的更多相关文章

  1. iOS 中隐藏UITableView最后一条分隔线

    如何优雅的隐藏UITableView中最后一条分割线? 这个问题是很常见,却又不太容易解决的. 可能通常的做法都是隐藏UITableView的分割线,自定义一条. 最近在使用弹出菜单的时候,同样遇到了 ...

  2. 【转】[教程]隐藏ActionBar中的MenuItem

    原文网址:http://blog.csdn.net/appte/article/details/12104823 有时候我们需要在不同的时候改变ActionBar中MenuItem的项数,或者隐藏某些 ...

  3. [教程]隐藏ActionBar中的MenuItem

    有时候我们需要在不同的时候改变ActionBar中MenuItem的项数,或者隐藏某些MenuItem,百度上找了很久没什好资料,还是Google了一下,StackOverFlow上有大神解决了. 先 ...

  4. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  5. 隐藏进程中的模块绕过IceSword的检测

    标 题: [原创] 隐藏进程中的模块绕过IceSword的检测 作 者: xPLK 时 间: 2008-06-19,17:59:11 链 接: http://bbs.pediy.com/showthr ...

  6. sed tr 去除PATH中的重复项

    最近发现由于自己不良的安装软件的习惯,shell的PATH路径包含了很多冗余的项.这里使用shell命令去除PATH的冗余项. export PATH=$(echo $PATH | sed 's/:/ ...

  7. ASP.NET Core 1.0 中的依赖项管理

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  8. [CareerCup] 2.1 Remove Duplicates from Unsorted List 移除无序链表中的重复项

    2.1 Write code to remove duplicates from an unsorted linked list.FOLLOW UPHow would you solve this p ...

  9. 在xib里,拖一个UIView到UITableView中作为tableHeaderView

    原贴地址:http://blog.csdn.net/haoxinqingb/article/details/41683881 内容 在xib里,拖一个UIView到UITableView中作为tabl ...

随机推荐

  1. 学习docker

    虚拟机下Ubuntu环境 1.sudo apt-get update 2.sudo apt-get install docker.io 3.在daocloud(http://www.daocloud. ...

  2. Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project LogTest: Compilation failure -> [Help 1]

      [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default ...

  3. Android studio 提示:Can't use Subversion command line client: svn Probably the path to Subversion executable is wrong. Fix it.

    1.参考来源:http://my.oschina.net/fyyy/blog/519353 按照下图,svn相关选项不要选.

  4. 关于springMVC3.0基于注解方式的项目搭建

    前言:开发了几个月的AS3项目,感觉JAVA都用不太熟练了.刚好这几个抽的空,就把自己以前用过的Spring框架再搭一边, 并完整的记录下来 开发环境:tomcat + mysql+ java 1.所 ...

  5. Service Broker应用(1):简介、同server不同DB间的数据传输

    简介:SQL Server Service Broker,以下简称SSB,是一种完全基于MSSQL数据库的数据处理技术,为短时间内处理大量数据提供了一种可靠.稳定.高效的解决方案.一次同步的数据最大可 ...

  6. memset函数详解

    语言中memset函数详解(2011-11-16 21:11:02)转载▼标签: 杂谈 分类: 工具相关  功 能: 将s所指向的某一块内存中的每个字节的内容全部设置为ch指定的ASCII值, 块的大 ...

  7. iptables原理详解(一)

    iptables简介 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它可以代替昂贵的商业防火 ...

  8. Java泛型学习笔记 - (五)泛型接口

    所谓泛型接口, 类似于泛型类, 就是将泛型定义在接口上, 其格式如下: public interface 接口名<类型参数>如: interface Inter<T> { pu ...

  9. 更改mysql中当前auto_increment的值的方法

    最近给自己网站更改mysql中当前auto_increment的值 如果在mysql中一个表test中的ID字段设为auto_increment插入两条记录后ID=2,这时删除1条记录,再插入一条变成 ...

  10. jQuery是什么?

    jQuery就是javascript的一个库,把我们常用的一些功能进行了封装,方便我们来调用,提高我们的开发效率. 极大地简化了 JavaScript 编程. Javascipt跟jQuery的区别: ...