how to set UITableView's scroll position to previous location when click “load earlier items” button

走远了吗. 提交于 2019-12-08 04:34:28

问题


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

When I scrolled tableView to top, my button appears and after I clicked the button, I fill earlier records and reload tableView.

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

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.

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

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

回答1:


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

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.




回答2:


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:

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


来源:https://stackoverflow.com/questions/24820717/how-to-set-uitableviews-scroll-position-to-previous-location-when-click-load-e

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!