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

  • 相关阅读:
    poj2623
    poj2635
    案例解析丨 Spark Hive 自定义函数应用
    云图说 | 华为云GPU共享型AI容器,让你用得起,用得好,用的放心
    云小课 |选定合适的证书,做“有证”的合规域名
    记一次 node 项目重构改进
    SpringBoot写后端接口,看这一篇就够了!
    如何让知识图谱告诉你“故障根因”
    我敢说,这个版本的斗地主你肯定没玩过?
    5 分钟带你掌握 Makefile 分析
  • 原文地址:https://www.cnblogs.com/qiutangfengmian/p/6074420.html
Copyright © 2020-2023  润新知