chalkandcheese
chalkandcheese

Reputation: 3

restore scroll position uitableview

I have a table view that leads to a detail view. When I click back from the table view I return to the top of the table. I would like it to return to the position of the selected row. I know I can use scroll to position but it needs an index path. Am I right in thinking I need to create a index path property and use that in the method call. If so, where do I populate it - in the did select row/cell method? I have sections in my table so would it be index path section, index path row? And finally is it to be called in view did appear?

Upvotes: 0

Views: 280

Answers (2)

Zaporozhchenko Oleksandr
Zaporozhchenko Oleksandr

Reputation: 4908

Maybe u looking for this:

tableView.contentOffset.y;

If you mean - you back from detail controller to ur master controller, u can scroll to position in viewWillAppear method (save tableView.contentOffset.y in viewWillDissapear). But i created new project and there no need to do this, it automatically open master controller with saved position when back from detail

Upvotes: 1

Mohd Prophet
Mohd Prophet

Reputation: 1521

It should work using - (void)scrollToRowAtIndexPath:(NSIndexPath *)indexPath atScrollPosition:(UITableViewScrollPosition)scrollPosition animated:(BOOL)animated using it this way:

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:selectedRow inSection:yourSection];
[yourTableView scrollToRowAtIndexPath:indexPath 
                     atScrollPosition:UITableViewScrollPositionTop 
                             animated:YES];

atScrollPosition could take any of these values:

typedef enum {
    UITableViewScrollPositionNone,
    UITableViewScrollPositionTop,
    UITableViewScrollPositionMiddle,
    UITableViewScrollPositionBottom
} UITableViewScrollPosition;

I hope this helps you

Upvotes: 0

Related Questions