Search code examples
objective-cmacosnsmutablearraycocoa-bindings

Table does not update entries after data is loaded into array


I have a service that loads data into my table view. When the service has loaded the data I load the data into my model:

[self.model.entries removeAllObjects];
[self.model.entries addObjectsFromArray:objects];

Now I am wondering if those two calls are "binding compliant" so that the table view will know that it has to update it's data which are directly bound to self.model.entries. I am asking this because after the service is done the table does not ask my delegate again for the number of tableviewcells so it seems that the binding does not kick in.

I think I could call reloadData on the tableview but this would then no longer conform to the "binding philosophy", right?

Please note that my model class returns a new NSMutableArray if it is not present. It is not clear to me if this is the correct approach for using bindings.


Solution

  • For full bindings compatibility you should bind through an NSController... in this case, presumably NSArrayController.

    By binding directly to the model you are breaking the MVC pattern, and no, I don't believe that the NSTableView will pick up changes when directly bound to the model.

    Bind your table view to NSArrayController's arrangedObjects, for example, and use the NSArrayController methods, such as:

    -add:
    -removeObjects:
    -insert:
    

    to manage the content. The table view will pick up on these changes automatically.