方法一:(只有一个headerView)一段

如果你的tableview恰好只有一个headerView,实现这种效果就好办了。把要设置的headerView设置成tableView的header而不是section = 0的headerView。

self.tableView.tableHeaderView = view;

方法二:

该方法比较简单,设置tableView的style为UITableViewStyleGrouped即可。代码如下

self.tableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds] style:UITableViewStyleGrouped];

当设置成grouped类型之后,头部会出现空白,在每一个section之间也会出现空白,解决方法如下(swift代码):

设置tableHeaderView的高度为一个比较小的值。

let view = UIView.init(frame: CGRect(x: 0, y: 0, width: self.view.frame.size.width, height:0.001))
         self.tableView.tableHeaderView = view

设置每一段的段尾值为一个比较小的值,去除空白。

func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
                   return 0.01
          }

方法三:

由于tableView是特殊的scrollView,在controller中加入如下代码:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{

       CGFloat sectionHeaderHeight = self.sectionHeight;

      // NSLog(@"%f,%f",scrollView.contentOffset.x,scrollView.contentOffset.y);

       if(scrollView == self.tableView){

         if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>= -64) {

          scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);

         } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {

            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);

        }

       }

     }

其中sectionHeight为你的tableview的section=0的headerView的高度。

由于我的tableViewController是在navigationController中使用的,而使用navigationController会使tableView(scrollView)的subView整体下移64,所以使用scrollView.contentOffset.y>=-64,如果没有使用navigationController就应该使用scrollView.contentOffset.y>=-64。这个根据自己的情况设置就好了。

tableview: 实现tableview 的 section header 跟随tableview滑动的更多相关文章

  1. iOS 解决tableView中headerView头部视图不跟随tableView滑动的方法

    解决方法如下: if (scrollView.contentOffset.y >= 0 && scrollView.contentOffset.y <= pushNewsT ...

  2. ios开发之--tableview单选/多选实现(非tableview的editing状态)及默认选中

    实现思路比较简单,这里仅做记录: 直接上代码: 1,实现didSelectRowAtIndexPath方法 -(void)tableView:(UITableView *)tableView didS ...

  3. iOS- 如何改变section header

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

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

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

  5. UITableView section header 不固定

    iOS系统自带的UITableView,当数据分为多个section的时候,在UITableView滑动的过程中,默认section header是固定在顶部的,滑动到下一个section的时候,下一 ...

  6. 设置TableView section header的颜色

      - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInte ...

  7. 取到 tableview 自定义section header 上的button

    在自定义的组头上,添加了一个button,在点击cell是想取到相应的组头上的button来进行操作时(比如说隐藏.是否响应点击事件等)时,我遇到了取不到所有button的问题,试过了常规的通过vie ...

  8. 【代码笔记】iOS-点击城市中的tableView跳转到旅游景点的tableView,下面会有“显示”更多。

    一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...

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

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

随机推荐

  1. 使用PHP脚本来写Daemon程序

    什么是Daemon进程   这又是一个有趣的概念,daemon在英语中是"精灵"的意思,就像我们经常在迪斯尼动画里见到的那些,有些会飞,有些不会,经常围着动画片的主人公转来转去,啰 ...

  2. iOS: 学习笔记, 添加一个带界面约束的控制器

    1. 创建一个空iOS应用程序(Empty Application). 2. 添加加控制器类. 修改控制器类的viewDidLoad - (void)viewDidLoad { [super view ...

  3. C#获取硬盘空间信息

    /// <summary> /// 获取指定驱动器的空间总大小(单位为B) /// </summary> /// <param name="str_HardDi ...

  4. Web Adaptor重装配置时 提示已经配置成功的问题

    环境 ArcGIS 10.1/10.2/10.3 Windwos 8.1 Tomcat 7.0.5 问题描述 较早之前在本机上安装配置过一个10.2.1版本的ArcGIS产品,包括桌面.Server和 ...

  5. windows下端口被占用的解决方法

    1:打开CMD输入:netstat -ano | findstr "80" 找到PID: 2:查看应用名称:tasklist | findstr "2544" ...

  6. 【解决】Maven myeclipse出现Could not find the main class

    maven报错:maven org/codehaus/plexus/classworlds/launcher/Launcher (Unsupported major.m...) 解决思路如下: 按照以 ...

  7. LA 3904

    一道DP题: 一共有三种砖,1*2,2*1,2*2,然后要你铺满整个n*2的地板,求不重复的铺法数: 方法: 首先计算了不考虑对称的情况,然后计算只考虑对称的情况: 所以结果就是(不考虑对称数+只考虑 ...

  8. Sed&awk笔记之awk篇

    http://blog.csdn.net/a81895898/article/details/8482333 Awk是什么 Awk.sed与grep,俗称Linux下的三剑客,它们之间有很多相似点,但 ...

  9. JavaScript如何获取css属性

    原文:JavaScript如何获取css属性 在web开发中,很多时候我们需要用JavaScript对网页的样式进行修改,接下来,我们就来看一下我们该如何访问css属性,针对不同情况及案例来进行分析. ...

  10. 【HDOJ】3205 Factorization

    题意很简单.就是求x^k-1的因式分解.显然x-1必然是其中之一(x=1, x^k-1=0).假设k=mp. 则x^k = (x^p)^m, 同理x^p-1必然是其中之一,即x^p的所有因式一定是x^ ...