Search code examples
core-datansarraycontrollernspopupbutton

NSTableView content based on a selection of another NSArrayController


The issue

I have a popup button (NSPopUpButton) that is bound to an NSArrayController. This array controller handles parent objects that each have a collection of child objects. I have an NSTableView in which I need to show these children for the selected item in popup. In addition, the list of children needs to be manipulated (add/remove).

I've tried to accomplish this in many ways but always run into some thing that complicates the solution. What is the best way to implement this?

The data is managed here by Core Data and thus, the collections are NSSets. I've tried adding a conversion method in the parent to return a sorted NSArray (in order to bind it with NSArrayController) but this approach prevents the KVO and the array controller is not updated properly.

Thank you in advance.

An example

To clarify, here's a hypothetic example:

Let's say I have a list of countries that is maintained elsewhere. One of these countries is selected in a popup button. Each country has a set of cities. When a country is selected a table view is populated by it's cities.


Solution

  • I didn't find any way to implement this by simply with dragging and dropping. I had to implement a delegate and data source for the table of cities (from the example). The window controller is notified of the selection changes in the popup button and this updates the content on the table view delegate / data source.

    I actually feel this is little bit better way to implement the issue (than with bindings and array controllers) since it gives more control over special cases.