让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会停在滚到的位置上。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-24 18:42:25

让UITableView的headerView或footerView跟随cell一起滚动的相关文章

iOS开发-- 如何让 UITableView 的 headerView跟随 cell一起滚动

在我们利用 UITableView 展示我们的内容的时候,我需要在顶部放一个不同于一般的cell的 界面,这个界面比较独特. 1. 所以我就把它 作为一个section的 headerView. 也就是在函数: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 里面返回 这个UIView. 但是,由于这个UIView占的空间很大,基本占用整个屏幕的高度,而滚动table

(转) 如何让 UITableView 的 headerView跟随 cell一起滚动

在我们利用 UITableView 展示我们的内容的时候,我需要在顶部放一个不同于一般的cell的 界面,这个界面比较独特. 1. 所以我就把它 作为一个section的 headerView. 也就是在函数: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section 里面返回 这个UIView. 但是,由于这个UIView占的空间很大,基本占用整个屏幕的高度,而滚动table

如何让 UITableView 的 headerView跟随 cell一起滚动

将UIView设置为  整个tableView的headerView,而不是 section 0的headerView self.tableView.tableHeaderView=header; 这样,就可以完美的满足 headerView跟随cell的内容一起滚动的要求拉. 结论:设置 UIView为  tableView的tableHeaderView即可实现 headerView跟随tableView一起滚动的效果

改变UITableView的headerView、footerView背景颜色

问题 改变UITableView的header.footer背景颜色,这是个很常见的问题.之前知道的一般做法是,通过实现tableView: viewForHeaderInSection:返回一个自定义的View,里面什么都不填,只设背景颜色.但是今天发现一个更简洁的做法. 更简洁的方法 对于iOS 6及以后的系统,实现这个新的delegate函数即可: - (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIVie

去掉UITableView HeaderView或FooterView随tableView 移动的黏性

去掉UITableView HeaderView或FooterView随tableView 移动的黏性(sticky) 控制器中实现以下方法即可: 1 - (void)scrollViewDidScroll:(UIScrollView *)scrollView { 2 3 CGFloat sectionHeaderHeight = 40; 4 5 if (scrollView.contentOffset.y<=sectionHeaderHeight&&scrollView.conte

UITableView, 用封装类的方法给cell赋值

#import "RootViewController.h" #import "Person.h" @interface RootViewController ()<UITableViewDataSource> @property (nonatomic, retain)NSMutableArray *array; @end @implementation RootViewController - (void)dealloc { [_array relea

ListView设置headerview和footerview

[简介]headerview就是通常看到的那种listview手势下滑露出上面的部分,下拉到一定位置,松手会开始请求网络数据,然后刷新listview的列表.footerview一般就是listview手势一直上滑动到显示出最后一条数据,然后继续按住滑动到一定位置,再松手,会加载下一页的数据.注:除ListView之外,其它像scrollview,webview的header和footer和listview基本一致. [属性]do平台的listview有4个属性来控制headerview和foo

iOS之UITableView带滑动操作菜单的Cell

制作一个可以滑动操作的 Table View Cell 本文翻译自 http://www.raywenderlich.com/62435/make-swipeable-table-view-cell-actions-without-going-nuts-scroll-views 原作者:Ellen Shapiro Apple 通过 iOS 7 的邮件(Mail)应用介绍了一种新的用户界面方案——向左滑动以显示一个有着多个操作的菜单.本教程将会向你展示如何制作一个这样的 Table View Ce

UITableVIew与UICollectionView带动画删除cell时崩溃的处理

-会崩溃的原因是因为没有处理好数据源与cell之间的协调关系- 效果: tableView的源码: ModelCell.h + ModelCell.m // // ModelCell.h // Set // // Created by YouXianMing on 14/11/24. // Copyright (c) 2014年 YouXianMing. All rights reserved. // #import <UIKit/UIKit.h> @class ModelCell; @pro