看起来第二个数组控制器无法工作,因为无法将两个阵列控制器连接到一个表视图。
这是绑定到树控制器(控制器密钥:选择,模型关键路径:allChildLeafs)阵列控制器被留在原处,但不限制于任何视图。
在Xcode中一个IBOutlet NSArrayController的被创建,然后连接到在界面生成器(IB)新创建的阵列控制器。此外,还有一个新的NSMutableArray被声明,包含setter和getter方法。接着,下面的代码被用于阵列控制器绑定到新的NSMutableArray:
[newArrayController bind:NSContentArrayBinding toObject:self withKeyPath:@"mutableArray" options:nil];
所以,现在的阵列控制器将“持有”无论是新的可变数组英寸通过将新的数组控制器连接到一个表视图,数组的内容可以显示在表视图中。
所有这一切都需要的是使这种可变数组包含每节车厢一个NSMutableDictionary对象。每个字典将有三个关键值对。三个键将是:“carName”,“mostPopularColor”,“secondMostPopularColor”。
由于旧的数组控制器持有当前在大纲视图中选择的“Car”对象的数组,因此首先获取“Car”对象的数组。要做到这一点,观察到在旧阵列控制器的arrangedObjects的变化,并使用观察“车”对象的新数组:
[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];
}}