iOS系统自带的UITableView,当数据分为多个section的时候,在UITableView滑动的过程中,默认section header是固定在顶部的,滑动到下一个section的时候,下一个section header把上一个section header顶出屏幕外。典型的应用就是通讯录。

默认情况下,UITableView的section header是固定的,如何让section header不固定呢?也就是随着UITableView的滑动而滑动,顶部不是一直都显示section header。方法是设置UITableView 的contentInset。代码如下:

  1. #pragma mark - scrollView代理函数
  2. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  3. {
  4. // 修改contentSize
  5. if([scrollView isKindOfClass:[UITableView class]]){// 不固定section
  6. CGFloat sectionHeaderHeight = pxToCoordinate(TABLECELL_SECTION_HEADER);
  7. if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=) {
  8. scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, , , );
  9. } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
  10. scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, , , );
  11. }
  12. }
  13. }

原理:当滑动的值大于section header的高度时,设置其contentInset,达到不显示header的效果,这样就类似于将header给顶出了屏幕;当滑动至小于section header的高度时,恢复contentInset,显示header。

需要注意:因为UITableView 滑动时contentOffset会不断的改变,因此该部分代码需要写到 scrollViewDidScroll 方法中。

UITableView section header 不固定的更多相关文章

  1. 让UITableView的section header view不悬停的方法

    当 UITableView 的 style 属性设置为 Plain 时,这个tableview的section header在滚动时会默认悬停在界面顶端.取消这一特性的方法有两种: 将 style 设 ...

  2. iOS- 如何改变section header

    希望这个从UITableViewDelegate协议里得到的方法可以对你有所帮助: - (UIView *) tableView:(UITableView *)tableView viewForHea ...

  3. 在Storyboard中为UITableView添加Header和Footer

    我在这里所说的Header和Footer并不是sectionHeader和sectionFooter,而是指UITableView的tableHeaderView和tableFooterView,这两 ...

  4. UITableView section 圆角 阴影

      在UITableView实现图片上面的效果,百度一下看了别人的实现方案有下面2种: 1.UITableView section里面嵌套UITableView然后在上面实现圆角和阴影,  弊端代码超 ...

  5. tableview: 实现tableview 的 section header 跟随tableview滑动

    方法一:(只有一个headerView)一段 如果你的tableview恰好只有一个headerView,实现这种效果就好办了.把要设置的headerView设置成tableView的header而不 ...

  6. 修改(table的section与上一个section的间距)section header背景颜色

    - (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView * ...

  7. 设置UITableView section间距

    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 0 ...

  8. iOS8中 UITableView section 分区头部视图不显示

    最近自己使用了UITableView写了一个通讯录,但是在编写过程还算顺利,但是后来测试的时候,发现在iOS8中TableView的分区头不能正常显示,使用 - (NSString *)tableVi ...

  9. ios8 UITableView section不显示

    ios8 如果UITableView只设置viewForHeaderInSection,则可能section不能显示,iOS7及以下版本显示正常. 解决方案: 设置heightForHeaderInS ...

随机推荐

  1. rsync不存在用户处理CPU消耗拒绝服务漏洞

    受影响产品: rsync 3.1.0 漏洞描述: CVE ID:CVE-2014-2855 rsync是一款文件同步管理软件. rsync处理不存在用户时存在安全漏洞,可消耗大量CPU资源,造成拒绝服 ...

  2. WebService教程和分析

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  3. virtualbox更新完之后重启不成功

    前几天更新完virtualbox,一直没用,今天想用,可是提示can't not access the kernel drivers,百度完之后按照别人博客所教方法弄好了,特地来转载他人文章,表达对博 ...

  4. Nosql释义

    NoSQL不是产品,是一项运动        ---->NoSQL(NoSQL = Not Only SQL ),意即反SQL运动,是一项全新的数据库革命性运动,早期就有人提出,发展至2009年 ...

  5. Zend Framework 入门(4)—页面布局

    Zend Framework 的页面布局模块——Zend_Layout——既可以跟 MVC 一起使用,也可以单独使用.本文只讨论与 MVC 一起使用的情况. 1. 布局脚本 在 application ...

  6. rpm 命令参数使用详解

    RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包 二进制包(Binary)以及源代码包(Source)两 ...

  7. POJ 3233 Matrix Power Serie

    题意:给一个n×n的矩阵A,求S = A + A2 + A3 + … + Ak. 解法:从式子中可得递推式S(n) = S(n - 1) + An,An = An-1×A,可得矩阵递推式 [S(n), ...

  8. C++ STL算法系列3---求和:accumulate

    该算法在numeric头文件中定义. 假设vec是一个int型的vector对象,下面的代码: //sum the elements in vec starting the summation wit ...

  9. UiThread DEMO

    import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import android.a ...

  10. linux常用命令之--文本编辑和文本内容查看命令

    linux的文本编辑和文本内容查看命令 1.文本编辑命令 vi:用于编辑文本文件,基本上可以分为三种模式,分别是一般模式.编辑模式.命令行模式. 一般模式:当编辑一个文件时,刚进入文件就是一般模式. ...