Search code examples
swiftuitableviewuiscrollviewuiscrollviewdelegate

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


Solution

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