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

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

#pragma mark - scrollView代理函数
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
// 修改contentSize
if([scrollView isKindOfClass:[UITableView class]]){// 不固定section
CGFloat sectionHeaderHeight = pxToCoordinate(TABLECELL_SECTION_HEADER);
if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=) {
scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, , , );
} else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, , , );
}
}
}

原理:当滑动的值大于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. UVa 10341 (二分求根) Solve It

    很水的一道题,因为你发现这个函数是单调递减的,所以二分法求出函数的根即可. #include <cstdio> #include <cmath> //using namespa ...

  2. HDU 1686 (KMP模式串出现的次数) Oulipo

    题意: 求模式串W在母串T中出现的次数,各个匹配串中允许有重叠的部分. 分析: 一开始想不清楚当一次匹配完成时该怎么办,我还SB地让i回溯到某个位置上去. 后来仔细想想,完全不用,直接让模式串向前滑动 ...

  3. UVA 11354 Bond(最小瓶颈路+倍增)

    题意:问图上任意两点(u,v)之间的路径上,所经过的最大边权最小为多少? 求最小瓶颈路,既是求最小生成树.因为要处理多组询问,所以需要用倍增加速. 先处理出最小生成树,prim的时间复杂度为O(n*n ...

  4. Java [Leetcode 328]Odd Even Linked List

    题目描述: Given a singly linked list, group all odd nodes together followed by the even nodes. Please no ...

  5. 【Python】实践笔记

    为什么要在脚本中加入? import sys reload(sys) sys.setdefaultencoding('utf-8')

  6. windows配置jdk

    一.JDK1.6下载 目前JDK最新版本是JDK1.6,到http://java.sun.com/javase/downloads/index.jsp可以下载JDK1.6. 二.JDK1.6安装 JD ...

  7. 移动设备3G网站制作的detail

    说明一下,在此所说的移动设备前端开发是指针对高端智能手机(如Iphone.Android),所以需要对webkit内核的浏览器有一定的了解. 1.webkit内核中的一些私有的meta标签 <m ...

  8. SharePoint 2010 列表项事件接收器 ItemAdded 的使用方法

    列表项事件处理器是继承于Microsoft.SharePoint.SPItemEventReceiver的类,Microsoft.SharePoint.SPItemEventReceiver类提供了许 ...

  9. Android常用组件

    UI相关 图片 Android-Universal-Image-Loader:com.nostra13.universalimageloader:异步加载.缓存.显示图片 ImageLoader:co ...

  10. JTA事务管理--配置剖析

    概述    [IT168 专稿]Spring 通过AOP技术可以让我们在脱离EJB的情况下享受声明式事务的丰盛大餐,脱离Java EE应用服务器使用声明式事务的道路已经畅通无阻.但是很大部分人都还认为 ...