I have a tree controller bound to an array, called "content". "content" is an array of model objects, called "Car". Each "Car" contains an NSString called "carName" and an NSMutableArray called "mostPopularColors". "mostPopularColors" contains NSMutableDictionary objects with keys like: "most popular", "second most popular", "third most popular" etc and values like: "red", "green", "blue" etc.
An outline view is bound to the tree controller arranged objects and displays the "carName" of every "Car" in "content". A separate table view lists every "carName" in one column. This is done by having an array controller bound to the tree controller (controller key: selection, model key path: allChildLeafs).The table column value is then bound to the array controller's arranged objects, model key path: carName.
In the table view, I want two other columns listing the most popular color and second most popular color respectively. So the final table should have three columns listing all the car names along with each car's two most popular colors.
I can access the car names as described but not the colours since they themselves are in arrays.
I have tried to make a second array controller and link it to the first but can't get it to work.
So in the end I want to be able to select a car or cars in the outline view and see all their names and top two colors of each in the table view.
It seems the second array controller did not work because it isn't possible to connect two array controllers to one table view.
The array controller that was bound to the tree controller (controller key: selection, model key path: allChildLeafs) was left in place but not bound to any view.
In Xcode an IBOutlet NSArrayController was created and then connected to a newly created array controller in Interface Builder (IB). Also, an new NSMutableArray was declared, with setter and getter methods. Then, the following code was used to bind the array controller to the new NSMutableArray:
[newArrayController bind:NSContentArrayBinding toObject:self withKeyPath:@"mutableArray" options:nil];
So now the array controller would "hold" whatever was in the new mutable array. The contents of the array could be displayed in a table view by connecting the new array controller to a table view.
All that was needed was to make this mutable array contain an NSMutableDictionary object for each car. Each dictionary would have three key value pairs. The three keys would be: "carName", "mostPopularColor", "secondMostPopularColor".
Since the old array controller held the array of "Car" objects currently selected in the outline view, this was done by first getting that array of "Car" objects. To do this, changes in the old array controller's arrangedObjects were observed and the new array of "Car" objects were observed using:
[oldArrayController addObserver:self forKeyPath:@"arrangedObjects" options:NSKeyValueObservingOptionNew context:nil];
To handle the observation and use the new array of "Car" objects to get the final array of dictionary objects the following method was implemented:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {if (object == selectedChildLeafsController)
{
if ([[object arrangedObjects] count] > 0)
{//make a new mutable array, here called "array", of dictionaries from your array of "Car" objects which is found in [object arrangedObjects] . And then something like...
[self setMutableArray: array];
[newArrayController bind:NSContentArrayBinding toObject:self withKeyPath:@"selectedBonds" options:nil];}else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}}