views:

873

answers:

4

I have a UITableViewController in my app, which is added to the view hierarchy directly. After the view appears, I want to scroll to a specific cell. My solution would be to call the code for scrolling in -[viewDidAppear].

According to Apple's docs I have to call the method manually:

If the view belonging to a view controller is added to a view hierarchy directly, the view controller will not receive this message. If you insert or add a view to the view hierarchy, and it has a view controller, you should send the associated view controller this message directly.

The question is: When is the right time to call it manually?

Calling it from the parent view controller's -[viewDidAppear] leads to a crash when I try to do the scrolling because apparently, the table view actually didn't yet appear and therefore thinks it has no sections to scroll to.

+1  A: 

Calling it from the parent controller's -viewDidAppear is usually your best bet.

If this results in problems if the child view controller isn't fully initialized yet, you may have another problem. Make sure your child view controller is completely "ready for action" after -viewWillAppear is called (which you can also call manually from the parent's -viewWillAppear)

Philippe Leybaert
+1  A: 

In -[viewDidAppear] on the tableview, called indeed from the parent view controller's -[viewDidAppear], you can call [tableView reloadData], this way you ensure that the tableView is loaded and ready.

luvieere
A: 

This is how I manually call viewWillAppear, viewDidAppear, viewWillDisappear, viewDidDisappear: here

and one of the view controllers I load this way, has the following viewWillAppear: (note that this is viewWillAppear since at the point that viewDidAppear is called, your view is viewable by the user)

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [wordListTable reloadData];
    [wordListTable scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
}
mahboudz
A: 

what's the crash error you get when you called it? it may be possible that you are not retaining your tableview correctly.

Jlam