zoukankan      html  css  js  c++  java
  • tableview: 实现tableview 的 section header 跟随tableview滑动

       方法一:(只有一个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, 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。这个根据自己的情况设置就好了。

  • 相关阅读:
    Linux下安装SVN服务端小白教程
    在 Linux 下搭建 Git 服务器
    Linux系统中安装软件的几种方式
    springboot系列(十)springboot整合shiro实现登录认证
    shiro系列五、shiro密码MD5加密
    springboot系列(九)springboot使用druid数据源
    springboot系列(七) 项目热加载
    访问网页出现DNS错误
    SpringBoot入门
    MyBatisCodeHelper-Pro插件破解版[2.8.2] 【拒绝度盘】
  • 原文地址:https://www.cnblogs.com/qiutangfengmian/p/6074420.html
Copyright © 2011-2022 走看看