似乎第二个阵列控制器不起作用,因为不可能将两个阵列控制器连接到一个表视图。
绑定到树控制器的数组控制器(控制器键:选择,模型键路径:allChildLeafs)保留在原处,但未绑定到任何视图。
在 Xcode 中创建了一个 IBOutlet NSArrayController,然后连接到 Interface Builder (IB) 中新创建的数组控制器。此外,还声明了一个带有 setter 和 getter 方法的新 NSMutableArray。然后,使用以下代码将数组控制器绑定到新的 NSMutableArray:
[newArrayController bind:NSContentArrayBinding toObject:self withKeyPath:@"mutableArray" options:nil];
所以现在数组控制器将“保存”新可变数组中的任何内容。通过将新的数组控制器连接到表格视图,可以在表格视图中显示数组的内容。
所需要的只是使这个可变数组包含每辆车的 NSMutableDictionary 对象。每个字典将具有三个键值对。这三个键是:“carName”、“mostPopularColor”、“secondMostPopularColor”。
由于旧的数组控制器持有当前在大纲视图中选择的“汽车”对象数组,因此这是通过首先获取“汽车”对象数组来完成的。为此,观察旧数组控制器的排列对象的变化,并使用以下方法观察新的“汽车”对象数组:
[oldArrayController addObserver:self forKeyPath:@"arrangedObjects" options:NSKeyValueObservingOptionNew context:nil];
为了处理观察并使用新的“汽车”对象数组来获取字典对象的最终数组,实现了以下方法:
- (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];
}}