views:

73

answers:

2

I have a top level UIViewController that contains a UITableView. The top level UIViewController instantiates a NavigationController, and pushes another UIViewController onto the NavigationController. I.E. we push into a new, second view. This second view has the usual "Back" button in the upper left hand corner to allow you to navigate back to the top level view.

Is it possible, when navigating back to the top level view from the second view, to redraw the UITableView in the top level view, using data generated in the second view, by calling cellForRowAtIndexPath in the top level and if so, how does one do this?

+2  A: 

All you need to do is this (in your UIViewController that has a UITableView). You don't have to worry about what happens at cell-level at this point.

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

Just add the code above to your controller, and you'll see that the right thing happens when you come back from your second view. The call to reloadData tells the table view that it needs to refresh its cells from your model, and everything else follows nicely.

Zoran Simic
Thank you so much, that worked perfectly. Most excellent!
James Testa
A: 

You could implement the methods of UINavigationControllerDelegate and create the logic for the refreshing when popping view controllers.

Another way to do this would be implementing on your table view controller some logic on viewWillAppear, to refresh based on the data of the popped view controller.

If you need to send data from the second view controller to the first view controller, remember that your second view controller "doesn't" know the existence of the previous view controller. It should be transparent for it. You should implement some protocol to make the second view controller send data to the first view controller. It would be a third option to this problem, since your first view controller would be the delegate and the second view controller would be sending info to the delegate, you could be preparing your table view (first view controller) to reload, based on the received data.

vfn