运行效果图
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.sectionHeaderHeight = 80;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.tableView)
{
CGFloat sectionHeaderHeight = 80;
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);
}
}
}
对于有分组的,既有头部也有尾部的,有些时候需要尾部不吸附,推荐使用下面的方法,不需要每次滑动都调用上面的代理方法
_disPlayTableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0,MTScreenWidth, kSectionHederHeight)];
_disPlayTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, MTScreenWidth, kSectionHederHeight)];
_disPlayTableView.contentInset = UIEdgeInsetsMake(-kSectionHederHeight, 0, -kSectionFooterHeight, 0);
下面的效果更好,不用每次滚动就调用代理方法
_disPlayTableView.contentInset = UIEdgeInsetsMake(<span style="font-family:Microsoft YaHei;">sectionHeight</span>, 0, 0, 0);
这样实际上市吧table向上移动了一部分隐藏在navigation下面