NSTableView 内容基于另一个 NSArrayController 的选择
问题
我有一个绑定到 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 NSSet
s. 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有一个解决方案,无需委托/数据源设置。
我的上下文是这样的:
name
。.m
和一个.h
)。 (如果您转到 File-New-File-CoreData-NSManagedObjectSubclass,Xcode 将为您编写它们。) 现在 ParentObject.children 可以访问 ParentObject 的 ChildObjectsNSArrayControllers
:ParentArrayController< /code> 和
ChildArrayController
。NSTableView
:ParentTable
和ChildTable
,每个都有一列用于name
。 (无论您使用 Popup 还是表格,只要它由NSArrayController
控制,都没有关系。)采取的步骤如下:
实体名称
及其各自的实体(父实体或子实体)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 attributename
..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.childrenNSArrayControllers
:ParentArrayController
andChildArrayController
.NSTableViews
:ParentTable
andChildTable
, each with one column forname
. (It should not matter whether you use a Popup or a table as long as it's controlled by aNSArrayController
.)The steps to take are as follows:
Entity Name
with their respective Entity (Parent or Child)ChildArrayController
s binding section under ControllerContent-ContentSet bind to theParentArrayController
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 theParents.m
class file.我没有找到任何方法来通过简单的拖放来实现这一点。我必须为城市表实现一个委托和数据源(来自示例)。窗口控制器会收到弹出按钮中的选择更改的通知,这会更新表视图委托/数据源上的内容。
实际上,我觉得这是实现该问题的更好方法(比使用绑定和数组控制器),因为它可以更好地控制特殊情况。
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.