更改UITableView节标题的默认滚动行为 [英] Change Default Scrolling Behavior of UITableView Section Header

查看:111
本文介绍了更改UITableView节标题的默认滚动行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableView有两个部分。它是一个简单的表视图。我使用viewForHeaderInSection创建这些头的自定义视图。到目前为止,这么好。



默认的滚动行为是当遇到一个节时,节头部会停留在导航栏下面,直到下一节滚动到视图。



我的问题是:我可以更改默认行为,以使节标题不会停留在顶部,而是在导航栏下滚动



谢谢。

解决方案

我解决这个问题的方法是根据<$ c $调整 contentOffset UITableViewControllerDelegate (扩展 UIScrollViewDelegate )中的 contentInset

   - (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat sectionHeaderHeight = 40;
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);
}
}

这里只有一个问题,






{注意:40应该是您的第0部分标题。如果你使用的数字大于你的section 0标题的高度,你会看到手指的感觉受到影响(尝试像1000,你会看到跳动行为是很奇怪的顶部)。如果数字符合您的0区头部高度,手指感觉似乎是完美或接近完美}。


I have a UITableView with two sections. It is a simple table view. I am using viewForHeaderInSection to create custom views for these headers. So far, so good.

The default scrolling behavior is that when a section is encountered, the section header stays anchored below the Nav bar, until the next section scrolls into view.

My question is this: can I change the default behavior so that the section headers do NOT stay anchored at the top, but rather, scroll under the nav bar with the rest of the section rows?

Am I missing something obvious?

Thanks.

解决方案

The way I solved this problem is to adjust the contentOffset according to the contentInset in the UITableViewControllerDelegate (extends UIScrollViewDelegate) like this:

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
       CGFloat sectionHeaderHeight = 40;
   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);
   }
}

Only problem here is that it looses a little bit of bounce when scrolling back to the top.


{NOTE: The "40" should be the exact height of YOUR section 0 header. If you use a number that is bigger than your section 0 header height, you'll see that finger-feel is affected (try like "1000" and you'll see the bounce behaviour is sort of weird at the top). if the number matches your section 0 header height, finger feel seems to be either perfect or near-perfect.}

这篇关于更改UITableView节标题的默认滚动行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆