简体   繁体   English

如何将NSOutlineView与多个核心数据实体作为组一起使用

[英]How to use NSOutlineView with multiple core data entities as groups

I have been looking into how to populate a Source List from core data using multiple entities. 我一直在研究如何使用多个实体从核心数据中填充源列表。 If I had one entity, I can follow documentation and use a NSTreeController to populate and bind my data to the NSOutlineView. 如果我有一个实体,则可以遵循文档并使用NSTreeController填充数据并将其绑定到NSOutlineView。

In my specific situation I have around 3 hard-coded groups, where each one of my groups would relate to a separate core data entity. 在我的特定情况下,我大约有3个硬编码组,其中每个组都与一个单独的核心数据实体相关。 I attempted to combine them but they all have relationships to different entities so I wouldn't be able to combine them all in one core data entity. 我试图合并它们,但是它们都与不同实体有关系,因此我无法将它们全部合并到一个核心数据实体中。 I ideally wanted to bind my 3 core data entities to my Source List where they look something like the following (Where Pens, Papers and Desks are all separate entities) : 理想情况下,我想将3个核心数据实体绑定到我的“源列表”,它们看起来类似于以下内容(其中笔,纸和书桌都是单独的实体):

在此处输入图片说明

Does anyone know of anyway I'd be able to use a controller to help mange and combine three entities with my Source List or would the best approach be to manually populate NSOutlineView? 有谁知道我能够使用控制器来帮助管理三个实体并将其与我的“源列表”合并,还是最好的方法是手动填充NSOutlineView? Can I use some sort of array of NSArrayControllers to populate my NSOutlineView? 我可以使用某种NSArrayControllers数组填充NSOutlineView吗? Is there a way I can override NSTreeController to take in data from three sources? 有没有一种方法可以覆盖NSTreeController来从三个来源获取数据?

As an experiment I created a project with an outline view and tree controller. 作为实验,我创建了一个具有大纲视图和树形控制器的项目。 The contents of the tree controller is an array of dictionary, the array controllers (in entity mode) are outlets: 树控制器的内容是一个字典数组,该数组控制器(在实体模式下)是出口:

treeController.content = [
    ["arrayController": penTreeArrayController, "name": "Pens"],
    ["arrayController": paperTreeArrayController, "name": "Papers"],
    ["arrayController": deskTreeArrayController, "name": "Desks"]
]

The tree controller is a subclass of NSTreeController : 树控制器是NSTreeController的子类:

override func childrenKeyPath(for node: NSTreeNode) -> String? {
    if node.representedObject is NSManagedObject {
        return nil
    }
    return "arrayController.arrangedObjects"
}

It seems to work. 它似乎有效。 When I add or remove a pen, the outline view automagically updates. 当我添加或删除笔时,轮廓视图会自动更新。 It doesn't work the other way around, I can't use the tree controller to add a pen. 反之亦然,我不能使用树控制器添加笔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM