Cocoa - 在表视图中显示嵌套数组
我有一个绑定到数组的树控制器,称为“内容”。 “content”是模型对象的数组,称为“Car”。每个“Car”包含一个名为“carName”的 NSString 和一个名为“mostPopularColors”的 NSMutableArray。 “mostPopularColors”包含 NSMutableDictionary 对象,其键如:“最流行”、“第二流行”、“第三流行”等,值如:“红色”、“绿色”、“蓝色”等。
大纲视图绑定到树控制器排列对象并在“content”中显示每个“Car”的“carName”。单独的表视图在一列中列出了每个“carName”。这是通过将数组控制器绑定到树控制器(控制器键:selection,模型键路径:allChildLeafs)来完成的。然后将表列值绑定到数组控制器的排列对象,模型键路径:carName。
在表格视图中,我想要另外两列分别列出最流行的颜色和第二流行的颜色。因此,决赛表应该有三列,列出所有汽车名称以及每辆车的两种最流行的颜色。
我可以访问所描述的汽车名称,但不能访问颜色,因为它们本身位于数组中。
我尝试制作第二个阵列控制器并将其链接到第一个阵列控制器,但无法使其工作。
因此,最后我希望能够在大纲视图中选择一辆或多辆汽车,并在表格视图中查看它们的所有名称以及每种汽车的前两种颜色。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎第二个数组控制器不起作用,因为无法将两个数组控制器连接到一个表视图。
绑定到树控制器的数组控制器(控制器键:选择,模型键路径:allChildLeafs)保留在原处,但未绑定到任何视图。
在 Xcode 中创建了一个 IBOutlet NSArrayController,然后连接到 Interface Builder (IB) 中新创建的数组控制器。此外,还声明了一个新的 NSMutableArray,其中包含 setter 和 getter 方法。然后,使用以下代码将数组控制器绑定到新的 NSMutableArray:
因此,现在数组控制器将“保存”新可变数组中的任何内容。通过将新的数组控制器连接到表视图,可以在表视图中显示数组的内容。
所需要的只是使这个可变数组包含每辆车的 NSMutableDictionary 对象。每个字典将具有三个键值对。这三个键是:“carName”、“mostPopularColor”、“secondMostPopularColor”。
由于旧的数组控制器保存当前在大纲视图中选择的“汽车”对象的数组,因此这是通过首先获取该“汽车”对象的数组来完成的。为此,观察旧数组控制器的排列对象的变化,并使用以下方法观察新的“Car”对象数组:
要处理观察并使用新的“Car”对象数组来获取最终的字典对象数组,请执行以下操作:方法已实施:
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:
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:
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: