在 UITableView 中滚动时移动视图 [英] Move a view when scrolling in UITableView
问题描述
我有一个 UIView
,下面有一个 UITableView
:
I have a UIView
with a UITableView
below it:
我想要做的是当用户开始在表格中滚动时让 UITableView
上方的视图向上移动(移开)以便为 UITableView
(再次向下滚动时向下滚动).
What I would like to do is to have the view above the UITableView
move up (out of the way) when the user starts scrolling in the table in order to have more space for the UITableView
(and come down when you scroll down again).
我知道这通常是通过表头视图完成的,但我的问题是我的表视图位于选项卡内(实际上它是使用 TTSliddingPageviewcontroller
实现的横向滚动页面视图).所以虽然我只有一个顶部 UIView
,但有三个 UITableView
.
I know that this is normally done with a table header view, but my problem is that my table view is inside a tab (actually it is a side-scrolling page view implemented using TTSliddingPageviewcontroller
). So while I only have one top UIView
there are three UITableView
s.
是否可以手动完成此操作?我的第一个想法是将所有内容都放在 UIScrollView
中,但根据 Apple 的文档,永远不应将 UITableView
放在 UIScrollView
中,因为这会导致不可预测的行为.
Is it possible to accomplish this manually? My first thought is to put everything in a UIScrollView
, but according to Apple's documentation one should never place a UITableView
inside a UIScrollView
as this leads to unpredictable behavior.
推荐答案
由于 UITableView
是 UIScrollView
的子类,你的表视图的委托可以接收 UIScrollViewDelegate
方法.
Since UITableView
is a subclass of UIScrollView
, your table view's delegate can receive UIScrollViewDelegate
methods.
在你的表视图的委托中:
In your table view's delegate:
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
static CGFloat previousOffset;
CGRect rect = self.view.frame;
rect.origin.y += previousOffset - scrollView.contentOffset.y;
previousOffset = scrollView.contentOffset.y;
self.view.frame = rect;
}
这篇关于在 UITableView 中滚动时移动视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!