zoukankan      html  css  js  c++  java
  • 让UITableView的headerView或footerView尾随cell一起滚动

    以headerView为例(footerView处理方式类似),下面四种方式均有独到之处: 

    1、无分区 最简单也最常见。将headerView设置为整个tableView的headerView,而不是 section 0 的headerView
    self.tableView.tableHeaderView = headerView。

    2、多个section 设置 tableView 的 style 为 UITableViewStyleGrouped。然后

    <span style="font-size:14px;">- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
        return 分区数;
    }
    - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
        if (section == 0) {
           <span>	</span>return 1; // 注意当某个分区不须要headerView时,也须要又一次设置高度。(Group时有默认高度)
        }
        return 170;
    }
    - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
        if (section == 0) {
            return nil;
        }
        return headerView; 
    }</span>

    这样确实能够让 headerView 。在滚动tableView的时候,尾随着cell的内容一起滚动。可是cell都被加上了边框。并且cell的水平显示范围变窄了。

          可是我们能够尝试调整cell的Frame。或者自己定义cell。

     

     3、多个section 将每一个分区第一个cell作为分区的headerView

           重写cell,就不贴代码了,写的代码较方法2几乎相同,可是效果很好。

    4、去掉UItableview headerview黏性 (不推荐,但思路不错,方法应该还能够再优化下)

    <span style="font-size:14px;">- (void)scrollViewDidScroll:(UIScrollView *)scrollView {  
        if (scrollView == self.tableView)  
        {  
            CGFloat sectionHeaderHeight = HeaderHeight;  
            if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.contentOffset.y>=0) {  
                scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);  
            } else if (scrollView.contentOffset.y>=sectionHeaderHeight) {  
                scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);  
            }  
        }  
    }</span>

    可是。当可滚动的位置太小,scrollView会停在滚到的位置上。






     

  • 相关阅读:
    .net 面试题 没事多看看。。。。
    分享一下我记忆23种设计模式的方法 <转。。>
    再次写给我们这些浮躁的程序员 《搜集的。。。》
    C#验证邮箱,电话,手机,数字,英文,日期,身份证,邮编,网址,IP类.. (转后整理)
    javascript和jquery使用技巧集
    jQuery 增加 删除 修改select option .
    设计模式(二)
    JavaScript string 字符串类型的扩展方法
    26个jQuery使用技巧
    jBPM开发入门指南(1)
  • 原文地址:https://www.cnblogs.com/wgwyanfs/p/7066845.html
Copyright © 2011-2022 走看看