Reload UITableView when navigating back?

I have a top level UIViewController that contains a UITableView . The top level of the UIViewController instantiates the NavigationController and pushes another UIViewController on the NavigationController . I.E. we put forward a new, second look. This second view has the usual back button in the upper left corner so you can return to the top level view.

Is it possible to go back to the upper level from the second view to redraw the UITableView in the upper level, using the data created in the second view, calling cellForRowAtIndexPath at the upper level, and if so, how to do it?

+52
objective-c iphone navigation
Sep 19 '10 at 23:06
source share
7 answers

All you have to do is this (in your UIViewController with a UITableView). You do not need to worry about what is happening at the cell level at the moment.

 - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.tableView reloadData]; // to reload selected cell } 

Just add the code above to the controller and you will see that this is correct when you return from your second view. The reloadData call indicates in the table view that it should update its cells from your model, and everything else should be fine.

+104
Sep 20 '10 at 0:08
source share

If you only want to reload the selected cell, override viewWillAppear: in your custom UITableViewController subclass like this:

 - (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 left clearsSelectionOnViewWillAppear on YES (the default), you should get the path to the specified column before calling super , which will clear the selection.

Additionally, @ZoranSimic's solution for calling [self.tablView reloadData] acceptable, as it is smaller than code and still efficient.

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

+10
Oct 15 '13 at 17:36
source share

In addition to Zoran Simic's answer, here is the Swift code:

 override func viewWillAppear(animated: Bool) { super.viewWillAppear(animated) tableView.reloadData() } 
+6
Dec 04 '15 at 2:59
source share

You can implement the UINavigationControllerDelegate methods and create logic to update when view controllers appear.

Another way to do this is to implement some logic on viewWillAppear on your table view controller to update the data based on the output controller.

If you need to send data from the second view controller to the first view controller, remember that your second view controller is β€œunaware” of the existence of the previous view controller. It should be transparent to him. You must implement some protocol so that the second data view controller transfers data to the first view controller. This will be the third version of this problem, since your first view controller will be a delegate, and the second view controller will send information to the delegate, you can prepare your table view (the first view controller) for a reboot based on the received data.

+5
Sep 20 '10 at 0:12
source share

viewWillAppear is called every time a view appears for the first time. Here is my solution to reload the view only after going back

I create one variable in the .h file my ViewController to verify that this is the first time I go to this ViewController or when I move back

 @property (nonatomic) BOOL isViewExist; 

After that in viewWillAppear

 -(void)viewWillAppear:(BOOL)animated{ if(!self.isViewExist){ self.isViewExist = YES; // check it is the first time you go to this ViewController -> don't reload anything }else{ [self.tableView reloadData]; // to reload the tableview data // or your can refresh anything in your ViewController as you want } } 

Hope for this help

+2
Apr 28 '16 at 7:19
source share

Maybe now it can help.

For Swift 2+

In your viewDidAppear method:

If you want to reload the whole table:

 tableView.reloadData() 

If you want to reload only the selected cell.

 let index = tableView.indexPathForSelectedRow if (index != nil){ self.tableView.reloadRowsAtIndexPaths([index], withRowAnimation: UITableViewRowAnimation.Automatic) } 
+1
Apr 16 '16 at 1:56 on
source share

Updated for Swift 3

 let index = tableView.indexPathForSelectedRow if (index != nil){ self.tableView.reloadRows(at: [index!], with: UITableViewRowAnimation.automatic) } 
-one
Aug 07 '16 at 0:15
source share



All Articles