xcode 单击“加载较早的项目”按钮时如何将 UITableView 的滚动位置设置为上一个位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24820717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to set UITableView's scroll position to previous location when click "load earlier items" button
提问by blckhm
I have a tableView like iOS iMessage
detail view. And also I've a button at the top of tableView which used by "load earlier messages".
我有一个类似于 iOSiMessage
详细视图的 tableView 。而且我在 tableView 顶部有一个按钮,用于“加载较早的消息”。
When I scrolled tableView to top, my button appears and after I clicked the button, I fill earlier records and reload tableView.
当我将 tableView 滚动到顶部时,我的按钮会出现,在我单击该按钮后,我会填充较早的记录并重新加载 tableView。
While the records are repopulating in tableView, scrolls position does not change.
当记录在 tableView 中重新填充时,滚动位置不会改变。
By the way, I tried to keep scroll previous position value and setcontentoffset
to previous CGPoint
. But I realized that the scrollposition always be 0 when I clicked button. So, after I setcontentoffset, scroll stays the top..
顺便说一句,我试图保持滚动前一个位置值和setcontentoffset
前一个CGPoint
。但是我意识到当我单击按钮时滚动位置始终为 0。所以,在我 setcontentoffset 之后,滚动保持在顶部..
I need to relocate scrollview's position to previous area.
我需要将滚动视图的位置重新定位到上一个区域。
--- Edit ---
- - 编辑 - -
I try to improve my method which "keeping previous location of scrollview".
我尝试改进我的方法,即“保留滚动视图的先前位置”。
And I found the solution and works great. I'll share for all;
我找到了解决方案并且效果很好。我会分享给大家;
add this to .h file: @property (nonatomic) CGFloat scrollsPreviousLocation;
and @synthesize scrollsPreviousLocation;
to .m file.
将此添加到 .h 文件:@property (nonatomic) CGFloat scrollsPreviousLocation;
和@synthesize scrollsPreviousLocation;
.m 文件。
Now, Here is the trick: I calculate the position of scroll from bottom.. Not from Top.
现在,这是诀窍:我从底部计算滚动的位置......而不是从顶部。
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
scrollsPreviousLocation = (self.tableView.contentSize.height - scrollView.contentOffset.y);
}
After the calculation process, Now I need to set content offset to previous location;
在计算过程之后,现在我需要将内容偏移量设置为以前的位置;
[tableView reloadData];
CGPoint offset = CGPointMake(0,tableView.contentSize.height - scrollsPreviousLocation);
[tableView setContentOffset:offset animated:NO];
回答by Kevin Hirsch
When you are on top of your table view before loading previous content, you are at 0 for contentOffset
. If you want to be able to stay at the same scroll position after reloading the table view with previous messages row above, you will have to do this :
当您在加载之前的内容之前位于表格视图的顶部时,对于contentOffset
. 如果您希望在使用上面的先前消息行重新加载表格视图后能够保持在相同的滚动位置,则必须执行以下操作:
// Save the contentSize of your table view before reloading it
CGFloat oldTableViewHeight = self.tableView.contentSize.height;
// Reload your table view with your new messages
// Put your scroll position to where it was before
CGFloat newTableViewHeight = self.tableView.contentSize.height;
self.tableView.contentOffset = CGPointMake(0, newTableViewHeight - oldTableViewHeight);
That's it !
就是这样 !
If your previous table view content size height was 10 and the new one was 19, you want to place your content offset to 9 (so 10 from the bottom like before the reloading) which is equal to 19 - 10
.
如果您之前的 table view 内容大小高度为 10,而新的高度为 19,您希望将内容偏移量设置为 9(因此像重新加载之前一样从底部开始 10),这等于19 - 10
.
回答by MJ.Zeng
the answer from @Kevin Hirsch work,but in auto layout mode ,remember to add tableView.layoutIfNeeded() ,or else you will get the estimate height. here is my code in swift:
@Kevin Hirsch 的答案是有效的,但在自动布局模式下,请记住添加 tableView.layoutIfNeeded() ,否则您将获得估计高度。这是我的快速代码:
// Save the contentSize of your table view before reloading it
let oldTableViewHeight = self.tableView.contentSize.height
// Reload your table view with your new messages
self.tableView.layoutIfNeed()
// Put your scroll position to where it was before
let newTableViewHeight = self.tableView.contentSize.height;
self.tableView.contentOffset = CGPointMake(0, newTableViewHeight - oldTableViewHeight);