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

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

问题描述

我有一个包含两个部分的 UITableView.这是一个简单的表格视图.我正在使用 viewForHeaderInSection 为这些标题创建自定义视图.到目前为止,一切都很好.

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?

谢谢.

推荐答案

我解决这个问题的方法是根据contentInset中的contentInset调整contentOffset>UITableViewControllerDelegate(扩展UIScrollViewDelegate)像这样:

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.

{注意:40"应该是您的第 0 部分标题的确切高度.如果您使用大于第 0 部分标题高度的数字,您会看到手指感觉受到影响(尝试像1000"一样,您会看到顶部的弹跳行为有点奇怪).如果数字与您的第 0 部分标题高度匹配,则手指感觉似乎是完美的或接近完美的.}

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

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