您的位置:首页 > 产品设计 > UI/UE

解决IOS中UITableViewStylePlain 时对headerview的隐藏

2014-04-22 11:01 639 查看
当设置table的style为UITableViewStylePlain,如果出现viewForHeaderInSection,那么当滑到当前section时这个headerview会在一直stick在tableview的顶部,这也是apple将其命名为header的原因,但有时候我们只需要自定义一个headerview显示一些不方便放到cell里面的view,那么自然而然,我们不想它一直显示在顶部。So how to make it move
off screen when scrolling down?

查了一些资料,解决方案也五花八门:

1. 直接setHidden, 这样只会留下一个空白的区域,高度还是原先的headerview高度;不行

2. 设置tableview的contentoffset 为headerview的高度,然后再贴上headerview,这样做的话也行。只是稍微麻烦点。如下:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 40;//Change as per your table header hight
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);
}
}


3. 将style改成Grouped,这样headerview就不会定在顶部了,但是grouped不好控制,因为每个section之间还是有一段footerview,而且就算将其高度设为0,还是有一小段空白的footer,这样又得重写tableview了,不介意这个高度的话就可以这样解决;

4. 个人目前认为比较好的方法是玩个小小的trick:在stackoverflow上有:http://stackoverflow.com/questions/8949318/scroll-uitableview-header/8949353#8949353

By implementing 
tableView:viewForHeaderInSection:
 for
section of index 
0
,
you can instead have the header as the first section that should disappear when it scrolls. In this way, it's not a header for the whole UITableView.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息