首页 > 如何才能让tableview的headerview以及footerview不随着tableview滚动

如何才能让tableview的headerview以及footerview不随着tableview滚动

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


那就不要实现成 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 的下面。


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


放到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的sectionView,你可以用table.tableFooterView = [[UIView alloc] init...];这样就不会随着表格的下移而移动了。

【热门文章】
【热门文章】