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。这个根据自己的情况设置就好了。

  • 相关阅读:
    secureCRT 6.5 ssh登陆openeuler提示交换秘钥不支持
    ediary电子日记本-力荐
    centos8.4 iso下载地址
    Manjaro Cutefish 安装体验类似macos风格
    脚本启动第一行提示sh/bash找不到
    securecrt双击克隆会话
    开源的window/linux远程连接工具-mRemoeteNG
    securecrt设置日志缓存
    怎么实现通过扫描二维码进行登录
    使用link rel="shortcut icon"为网页标题加图标
  • 原文地址:https://www.cnblogs.com/qiutangfengmian/p/6074420.html
Copyright © 2011-2022 走看看