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

  • 相关阅读:
    383. Ransom Note
    598. Range Addition II
    453. Minimum Moves to Equal Array Elements
    492. Construct the Rectangle
    171. Excel Sheet Column Number
    697. Degree of an Array
    665. Nondecreasing Array
    视频网站使用H265编码能提高视频清晰度吗?
    现阶段的语音视频通话SDK需要解决哪些问题?
    企业远程高清会议平台视频会议系统在手机端使用的必备要求有哪些?
  • 原文地址:https://www.cnblogs.com/qiutangfengmian/p/6074420.html
Copyright © 2011-2022 走看看