单击“加载较早的项目"时如何将 UITableView 的滚动位置设置为上一个位置按钮 [英] how to set UITableView's scroll position to previous location when click "load earlier items" button

查看:19
本文介绍了单击“加载较早的项目"时如何将 UITableView 的滚动位置设置为上一个位置按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于 iOS iMessage 详细视图的 tableView.而且我在 tableView 顶部有一个按钮,用于加载较早的消息".

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".

当我将 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 中重新填充,但滚动位置不会改变.

While the records are repopulating in tableView, scrolls position does not change.

顺便说一下,我尝试将上一个位置值和 setcontentoffset 滚动到上一个 CGPoint.但是我意识到单击按钮时滚动位置始终为 0.所以,在我 setcontentoffset 之后,滚动保持在顶部..

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..

我需要将滚动视图的位置重新定位到上一个区域.

I need to relocate scrollview's position to previous area.

--- 编辑---

我尝试改进保留滚动视图的先前位置"的方法.

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;

将此添加到 .h 文件中:@property (nonatomic) CGFloat scrollsPreviousLocation;@synthesize scrollsPreviousLocation; 到 .m 文件.

add this to .h file: @property (nonatomic) CGFloat scrollsPreviousLocation; and @synthesize scrollsPreviousLocation; to .m file.

现在,诀窍是:我从底部计算滚动的位置......不是从顶部.

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];

推荐答案

当您在加载之前的内容之前位于表格视图的顶部时,对于 contentOffset,您位于 0.如果您希望在使用上面的先前消息行重新加载表格视图后能够保持在相同的滚动位置,则必须这样做:

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 :

// 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);

就是这样!

如果你之前的 table view 内容大小高度是 10 而新的高度是 19,你想把你的内容偏移量设置为 9(所以像重新加载之前一样从底部开始 10),它等于 19 - 10.

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.

这篇关于单击“加载较早的项目"时如何将 UITableView 的滚动位置设置为上一个位置按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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