views:

31

answers:

1

UITableView setting contentOffset in UIViewController viewDidLoad doesn't take effect...

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;

    self.tableView.contentOffset = CGPointMake(0.0f, 200.0f);
}

...the value is set, however, after setting an observer on the contentOffset of the table view I can see it is set to zero at least once more after viewDidLoad has run (this is in an empty iphone "table view" template project to ensure its not something I'm doing elsewhere).

I would use viewWillAppear: but that has the effect of losing the position of the users scroll if they are returning back to the controller from elsewhere.

Any ideas? I'm sure this must be simple :)

+2  A: 

In viewWillAppear, could you check if the offset is 0,0, and if it is, then set it to where you want it?

That would preserve the user's location while allowing your override.

Michael Kernahan
Hey Michael, yes, this is the interim measure I'm using, but somehow it doesn't feel as elegant as it should... I wonder if this is the approach Apple take in the Mail app for example to hide the search bar.
Tricky
Well, you're basically using the information to extract if the user has scrolled or not. You could also have a boolean that you flip to YES the first time the user scrolls the view. That might be optimal since the user may scroll to (0,0) and not like that it goes back to (0,200) when they return to the view.
Michael Kernahan