xcode reloading tableview

24,917

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 the reloadData message. 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 tableView property set up, then the simplest way of reloading the data from another view controller is to simply send the table view object the reloadData message directly. So, for example, if a method in ViewControllerFoo has a pointer to ViewControllerBar called bar and 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 reloadData method 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.

Share:
24,917
user1110415
Author by

user1110415

Updated on March 04, 2020

Comments

  • user1110415
    user1110415 over 4 years

    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.

  • user1110415
    user1110415 over 12 years
    thanks very much for the response. But I'm still unclear where exactly to place the reload method in my code. When I put it in [viewDidLoad], my tableview seemed to completely bypass the two methods you mentioned.
  • Mike Mertsock
    Mike Mertsock over 12 years
    Are your table view's delegate and dataSource properties set correctly?