Cocoa - 在表视图中显示嵌套数组

发布于 2024-12-18 10:07:04 字数 622 浏览 4 评论 0原文

我有一个绑定到数组的树控制器,称为“内容”。 “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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

二智少女 2024-12-25 10:07:04

似乎第二个数组控制器不起作用,因为无法将两个数组控制器连接到一个表视图。

绑定到树控制器的数组控制器(控制器键:选择,模型键路径:allChildLeafs)保留在原处,但未绑定到任何视图。

在 Xcode 中创建了一个 IBOutlet NSArrayController,然后连接到 Interface Builder (IB) 中新创建的数组控制器。此外,还声明了一个新的 NSMutableArray,其中包含 setter 和 getter 方法。然后,使用以下代码将数组控制器绑定到新的 NSMutableArray:

[newArrayController bind:NSContentArrayBinding toObject:self withKeyPath:@"mutableArray" options:nil];

因此,现在数组控制器将“保存”新可变数组中的任何内容。通过将新的数组控制器连接到表视图,可以在表视图中显示数组的内容。

所需要的只是使这个可变数组包含每辆车的 NSMutableDictionary 对象。每个字典将具有三个键值对。这三个键是:“carName”、“mostPopularColor”、“secondMostPopularColor”。

由于旧的数组控制器保存当前在大纲视图中选择的“汽车”对象的数组,因此这是通过首先获取该“汽车”对象的数组来完成的。为此,观察旧数组控制器的排列对象的变化,并使用以下方法观察新的“Car”对象数组:

[oldArrayController addObserver:self forKeyPath:@"arrangedObjects" options:NSKeyValueObservingOptionNew context:nil];

要处理观察并使用新的“Car”对象数组来获取最终的字典对象数组,请执行以下操作:方法已实施:

- (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];
}}

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];
}}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文