JimmyLee
JimmyLee

Reputation: 569

How to get UITableView last scroll position before show next UIViewController?

I have a question about UITableView.

Because I want to save the UITableView last position in my userdefault before showing next UIViewController.

And when I back to homePage I can read userdefault value to auto scroll to my saving position.

Why I ask this question, because when I back to homePage I'll refresh my viewcontroller and the layout will change, so I can't get exactly position before showing.

Have any idea to me? Thanks

Upvotes: 1

Views: 159

Answers (2)

manikal
manikal

Reputation: 973

By using contentOffset.

Save it on leaving:

float savedVerticalContentOffset = tableView.contentOffset.y;

and then when you return and refresh just call:

[tableView setContentOffset:CGPointMake(0, savedVerticalContentOffset)];

Upvotes: 2

Paul
Paul

Reputation: 2057

You could get the index path of the first visible cell with indexPathsForVisibleRows and store that. Then when you show the table again you could use scrollToRowAtIndexPath:atScrollPosition:animated: to make sure that cell is visible.

Upvotes: 1

Related Questions