In my app I have a tableview being filled by values from an NSMutableArray. This mutable array is being added to in a separate viewcontroller, and so I have to reload the data.
This is probably a very simple question, but I have reading 15+ posts and none of them seem to address xcode 4.2, and where exactly to put the [self.tableView reloadData]. Where exactly should this method be put? When I have tried putting it in various places in the tableviews source code, the tableview initialized for the first time with no values in it.
Or should it be put in the other viewcontrollers code, in which case how can I reference the particular instance of my UItableview subclass that is being used?
Thanks very much in advance.
There is nothing special about Xcode 4.2 that should make any difference here, so you should pay attention to other sources you have found, even if they refer to other versions of Xcode.
It doesn’t have to be
[self.tableView reloadData]exactly. You send the table view object thereloadDatamessage. How you access that table view object – through a property on self, through an instance variable, through a property in another class, etc. – is up to you.If you already have the
tableViewproperty set up, then the simplest way of reloading the data from another view controller is to simply send the table view object thereloadDatamessage directly. So, for example, if a method inViewControllerFoohas a pointer toViewControllerBarcalledbarand knows it should reload its table view, it could call[bar.tableView reloadData].If you don’t have the property set up, you can create it yourself, or you could create a
reloadDatamethod on the view controller holding the table view that does it on behalf of other view controllers.However these approaches mix up logic and presentation, which is usually a pretty poor architecture. If one view controller knows that another view controller should be updating its view, then chances are, you should be factoring out some of that logic to a third class that is independent of any particular view controller. That third class can transmit notifications, or your view controllers can listen for changes to its state via KVO.