ios - 如何才能让tableview的headerview以及footerview不随着tableview滚动
天蓬老师
天蓬老师 2017-04-17 13:43:40
[iOS讨论组]

tableview的headerview和footer view默认是随着tableview滚动的,现在要求他不随着section滚动,请问我该怎么做呢?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

全部回复(5)
怪我咯

随着表格一起滑动的是tableview的sectionView,你可以用table.tableFooterView = [[UIView alloc] init...];这样就不会随着表格的下移而移动了。

PHPz

那就不要实现成 tableView 的 headerView 了呗。
以 headerView 为例,在最前面添加一个 section,然后把这个 headerView 作为 section 的 header,这个 section 的行数为1(听说为0的时候偶尔会有些bug),行高为0,cellForRow里随便返回一个默认 style 的 cell即可。

update:仔细想了想,这样只能保证这个 section 在顶部的时候不滚动,否则还是会滚动出去。
真正的解决方法是:把 headerView 和 footerView 不要放在 tableView 里了,而是把 tableView 的高度缩短,把 headerView 放在 tableView 的上面,footerView 放在 tableView 的下面。

大家讲道理

放到view上就行了吧。

黄舟

-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

if (scrollView.tag == 100102) {
    UITableView *tableview = (UITableView *)scrollView;
    CGFloat sectionHeaderHeight = TABHEADER;
    CGFloat sectionFooterHeight = 10;
    CGFloat offsetY = tableview.contentOffset.y;
    if (offsetY >= 0 && offsetY <= sectionHeaderHeight)
    {
        tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -sectionFooterHeight, 0);
    }else if (offsetY >= sectionHeaderHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight)
    {
        tableview.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, -sectionFooterHeight, 0);
    }else if (offsetY >= tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight && offsetY <= tableview.contentSize.height - tableview.frame.size.height)
    {
        tableview.contentInset = UIEdgeInsetsMake(-offsetY, 0, -(tableview.contentSize.height - tableview.frame.size.height - sectionFooterHeight), 0);
    }
}

}

迷茫

其实如果只是想做一个不随tableview滑动的view有两种方法
1.view加在NavigationController上,这样view就不会随tableview滑动了
2.view加在tableview上,但是动态调整这个view的坐标,让他的坐标一直是0。(涉及一个小算法)

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号