NSTableView 内容基于另一个 NSArrayController 的选择

发布于 2024-10-26 05:00:47 字数 533 浏览 5 评论 0原文

问题

我有一个绑定到 NSArrayController 的弹出按钮 (NSPopUpButton)。该数组控制器处理每个具有子对象集合的父对象。我有一个 NSTableView,我需要在其中显示弹出窗口中所选项目的这些子项。此外,还需要操作子级列表(添加/删除)。

我尝试通过多种方式来实现这一目标,但总是遇到一些使解决方案变得复杂的事情。实现这一点的最佳方法是什么?

此处的数据由 Core Data 管理,因此集合是 NSSet。我尝试在父级中添加转换方法以返回排序的 NSArray(以便将其与 NSArrayController 绑定),但这种方法会阻止 KVO 和数组控制器没有正确更新。

先感谢您。

示例

为了澄清这一点,这里有一个假设的示例:

假设我有一个在其他地方维护的国家/地区列表。在弹出按钮中选择这些国家/地区之一。每个国家都有一组城市。选择一个国家/地区后,表视图中将填充该国家/地区的城市。

The issue

I have a popup button (NSPopUpButton) that is bound to an NSArrayController. This array controller handles parent objects that each have a collection of child objects. I have an NSTableView in which I need to show these children for the selected item in popup. In addition, the list of children needs to be manipulated (add/remove).

I've tried to accomplish this in many ways but always run into some thing that complicates the solution. What is the best way to implement this?

The data is managed here by Core Data and thus, the collections are NSSets. I've tried adding a conversion method in the parent to return a sorted NSArray (in order to bind it with NSArrayController) but this approach prevents the KVO and the array controller is not updated properly.

Thank you in advance.

An example

To clarify, here's a hypothetic example:

Let's say I have a list of countries that is maintained elsewhere. One of these countries is selected in a popup button. Each country has a set of cities. When a country is selected a table view is populated by it's cities.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你的他你的她 2024-11-02 05:00:47

有一个解决方案,无需委托/数据源设置。

我的上下文是这样的:

  • 带有父母和孩子的 CoreData 模型,一个父母通过名为孩子的关系拥有多个孩子。两者都有一个属性name
  • 这两个实体必须作为类提供(每个实体都有一个 .m 和一个 .h)。 (如果您转到 File-New-File-CoreData-NSManagedObjectSubclass,Xcode 将为您编写它们。) 现在 ParentObject.children 可以访问 ParentObject 的 ChildObjects
  • 两个 NSArrayControllersParentArrayController< /code> 和 ChildArrayController
  • 两个 NSTableViewParentTableChildTable,每个都有一列用于 name。 (无论您使用 Popup 还是表格,只要它由 NSArrayController 控制,都没有关系。)

采取的步骤如下:

  • 像往常一样将两个 NSArrayController 连接到 MangaggedObjectContext 并将它们设置为模式:实体名称及其各自的实体(父实体或子实体)
  • 照常将两个 TableView(其列)绑定到其 NSArrayController。
  • 现在魔术来了:在 ControllerContent-ContentSet 下的 ChildArrayController 绑定部分中,使用 ControllerKey: selection 和 ModelKeyPath: 绑定到 ParentArrayController孩子们。

完毕。如果您在 ParentTable 中选择 ParentObject,则 ChildTable 将显示其子项。

要向父级添加或删除子级,您可以使用 Xcode 在 Parents.m 类文件中为您编写的 (void)addChildrenObject:(Child *)value; 方法。

There is a solution for this without the delegate/datasource setup.

My context is this:

  • CoreData model with Parents and Children, one Parent has multiple Children via a relationship named children. Both have a attribute name.
  • The two Entities must be available as classes (each with a .m and a .h). (Xcode will write them for you if you go to File-New-File-CoreData-NSManagedObjectSubclass.) Now the ChildObjects of a ParentObject can be accessed by ParentObject.children
  • Two NSArrayControllers: ParentArrayController and ChildArrayController.
  • Two NSTableViews: ParentTable and ChildTable, each with one column for name. (It should not matter whether you use a Popup or a table as long as it's controlled by a NSArrayController.)

The steps to take are as follows:

  • Connect both NSArrayControllers to the MangagedObjectContext as usual and set them to Mode: Entity Name with their respective Entity (Parent or Child)
  • Bind both TableViews (their columns) to their NSArrayController as usual.
  • Now comes the magic: In the ChildArrayControllers binding section under ControllerContent-ContentSet bind to the ParentArrayController with ControllerKey: selection and ModelKeyPath: children.

Done. If you select a ParentObject in the ParentTable the ChildTable shows its children.

To add and remove children to parents you can use the (void)addChildrenObject:(Child *)value; method that Xcode wrote for you in the Parents.m class file.

骄傲 2024-11-02 05:00:47

我没有找到任何方法来通过简单的拖放来实现这一点。我必须为城市表实现一个委托和数据源(来自示例)。窗口控制器会收到弹出按钮中的选择更改的通知,这会更新表视图委托/数据源上的内容。

实际上,我觉得这是实现该问题的更好方法(比使用绑定和数组控制器),因为它可以更好地控制特殊情况。

I didn't find any way to implement this by simply with dragging and dropping. I had to implement a delegate and data source for the table of cities (from the example). The window controller is notified of the selection changes in the popup button and this updates the content on the table view delegate / data source.

I actually feel this is little bit better way to implement the issue (than with bindings and array controllers) since it gives more control over special cases.

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