Remembering Scroll Position on Uitableview

Remembering scroll position on UITableView

Sorted!

With a bit of inspiration from Desdenova, whilst at work I had a good think about it and realised what it could be. Remembering that I had a search bar, I had implemented the following code a few months ago to hide it:

[self.tableView setContentOffset:CGPointMake(0,-20) animated:NO];

In naivety I put that in viewDidAppear rather than viewDidLoad. Obviously, this did hide the search bar, but with it being in the wrong void command it did it every time the masterDetailView returned to the top of the stack. So, after moving the above code to viewDidLoad it now still hides it, but only the once.

I'm just spelling it out like this for other beginners, like myself, who may come across the same problem and may just save their sanity!

Thanks to you all for your ideas that helped me out.

+1 to you all!

How to remember exact scroll position in table view

QUESTION: I just saw that you mentioned that contentOffset wasn't working for you. Can you try the method again by printing out the contentOffset value each time? That's a good starting point to understand why the offset method is not working for you.

Use the following to save the scroll position:

 let offset = tableView.contentOffset

Then set the scroll position as follows:

tableView.setContentOffset(offset, animated: false)

How to reset the scroll position of a UITableView?

August got the UITableView-specific method. Another way to do it is:

[tableView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO];

This method is defined in UIScrollView, the parent class to UITableView. The above example tells it to scroll to the 1x1 box at 0,0 - the top left corner, in other words.

How can I get the UITableView scroll position so I can save it?

You can easily grab the exact offset of the table view by looking at its contentOffset property. For the vertical scroll, look at:

tableView.contentOffset.y;

Setting scroll position in UITableView

I think I've got a similar app: A list of item., tap '+' -> new screen, come back and see the updated list, scrolled to show the added item at the bottom.

In summary, I put reloadData in viewWillAppear:animated: and scrollToRowAtIndexPath:... in viewDidAppear:animated:.

// Note: Member variables dataHasChanged and scrollToLast have been
// set to YES somewhere else, e.g. when tapping 'Save' in the new-item view.

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (dataHasChanged) {
self.dataHasChanged = NO;
[[self tableView] reloadData];
} else {
self.scrollToLast = NO; // No reload -> no need to scroll!
}
}

- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (scrollToLast) {
NSIndexPath *scrollIndexPath = [NSIndexPath indexPathForRow:([dataController count] - 1) inSection:0];
[[self tableView] scrollToRowAtIndexPath:scrollIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:YES];
}
}

I hope this helps. If you add something in the middle of the list, you could easily scroll to that position instead.

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

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.

Preserving (and loading) table view scroll position for each data source

The position to save:

preservedPosition = tableView.visibleRect.origin

Restore:

tableView.scroll(preservedPosition)


Related Topics



Leave a reply



Submit