我最近工作中的一个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. Proxy Pattern(Java动态代理和cglib的实现)

    代理模式:给某一个对象提供代理对象,由代理对象控制具体对象的引用. 代理,指的就是一个角色对表另一个角色采取行动,就生活中,一个红酒厂商,是不会直接把红酒零销给客户的,都是通过代理完成他的销售业务.而 ...

  2. 关于Thinkphp Upload类

    $this->uploads($picurl); public function uploads($picurl) { $config = array( 'maxSize' => 3145 ...

  3. Scipy学习笔记 矩阵计算

    Scipy学习笔记 非本人原创  原链接 http://blog.sina.com.cn/s/blog_70586e000100moen.html 1.逆矩阵的求解 >>>impor ...

  4. 使用git删除远程仓库文件

    git rm -r -f --cached 文件或文件夹 git commit -m "移除文件或文件夹" git push origin master 注意:要删除的文件或文件夹 ...

  5. Java对象的XML序列化(转)

    转自:http://westlifesz.javaeye.com/blog/48618 java.io.Serializable引发的问题——什么是序列化?在什么情况下将类序列化?  序列化就是一种用 ...

  6. 在CentOS6.8上面安装Python3.5

    以前每次装Linux,升级Python,都会一堆问题,然后Google,本来想着记录一下,结果问题太多了,也就记不住了,这次特地记了下来. 在CentOS6.8上面安装Python3.5我的系统是Ce ...

  7. JS中的属性和变量的区别

    在很多文章中都说变量其实就是属性,但是它们之间有一定的区别,例如: 在全局作用域下, var a = "hello"; b = "hello"; 从字面上看,它 ...

  8. Ubuntu下freeradius-server的安装

    一.安装 (1)更新 #apt-get update (2)下载 链接:ftp://ftp.freeradius.org/pub/freeradius/freeradius-server-2.2.9. ...

  9. JQuery基础总结下

    JQuery动画与特效 show()和hide()方法可以用来显示和隐藏元素,toggle()方法用来切换显示和隐藏. $(selector).hide(speed,[callback]); $(se ...

  10. mseed2sac的安装和使用

    由于使用rdseed提取mseed文件到SAC文件会遇到一个问题就是: 同时需要dataless文件: 因此如果下载的数据中恰巧没有dataless文件,则需要用另外一种方式:mseed2sac 现在 ...