Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload UITableView when navigating back?

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?

like image 906
James Testa Avatar asked Sep 19 '10 23:09

James Testa


3 Answers

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]; // to reload selected cell
}

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.

like image 143
Zoran Simic Avatar answered Nov 19 '22 15:11

Zoran Simic


If you only want to reload the cell that was selected, override viewWillAppear: in your custom subclass of UITableViewController like so:

- (void)viewWillAppear:(BOOL)animated
{
    NSIndexPath *selectedRowIndexPath = [self.tableView indexPathForSelectedRow];
    [super viewWillAppear:animated]; // clears selection
    if (selectedRowIndexPath) {
        [self.tableView reloadRowsAtIndexPaths:@[selectedRowIndexPath] withRowAnimation:UITableViewRowAnimationNone];
    }
}

NOTE: Assuming you've left clearsSelectionOnViewWillAppear set to YES (the default), you must get the index path of the selected row before calling super, which clears the selection.

Also, the solution of @ZoranSimic to just call [self.tablView reloadData] is acceptable as it's less code and still efficient.

Finally, perhaps the best way to keep your table view's cells in sync with the model objects they represent is to do like NSFetchedResultsController and use key-value observing (KVO) and/or NSNotification to inform your table view controller when model objects have changed so that it can reload the corresponding cells. The table view controller could begin observing changes to its model objects in viewDidLoad and end observing in dealloc (and anywhere you manually unload self.view).

like image 14
ma11hew28 Avatar answered Nov 19 '22 13:11

ma11hew28


In addition to the answer of Zoran Simic here the Swift code:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    tableView.reloadData()
}
like image 9
Michael Dorner Avatar answered Nov 19 '22 13:11

Michael Dorner