zoukankan      html  css  js  c++  java
  • UITableView section header 不固定

    iOS系统自带的UITableView,当数据分为多个section的时候,在UITableView滑动的过程中,默认section header是固定在顶部的,滑动到下一个section的时候,下一个section header把上一个section header顶出屏幕外。典型的应用就是通讯录。

    默认情况下,UITableView的section header是固定的,如何让section header不固定呢?也就是随着UITableView的滑动而滑动,顶部不是一直都显示section header。方法是设置UITableView 的contentInset。代码如下:

    #pragma mark - scrollView代理函数
    - (void)scrollViewDidScroll:(UIScrollView *)scrollView
    {
        // 修改contentSize
        if([scrollView isKindOfClass:[UITableView class]]){// 不固定section
            CGFloat sectionHeaderHeight = pxToCoordinate(TABLECELL_SECTION_HEADER);
            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);
            }
        }
    }

    原理:当滑动的值大于section header的高度时,设置其contentInset,达到不显示header的效果,这样就类似于将header给顶出了屏幕;当滑动至小于section header的高度时,恢复contentInset,显示header。

    需要注意:因为UITableView 滑动时contentOffset会不断的改变,因此该部分代码需要写到 scrollViewDidScroll 方法中。

  • 相关阅读:
    APP_DEBUG作用
    tp字段映射机制原理
    unix、windows、mac 的换行习惯
    DroidCam 一片 红色 解决办法
    A3 A8 算法,中文简明解释
    /usr/local/mysql/bin/mysql P 3307 protocol=tcp 无法连接mysql
    [转载]网络基础:精解传输层安全协议
    无线网络加密一点漫谈
    安全模式:J2EE、Web服务和身份管理最佳实践与策略
    scp和winscp
  • 原文地址:https://www.cnblogs.com/acBool/p/5598420.html
Copyright © 2011-2022 走看看