简体   繁体   中英

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.

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. 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) :

在此处输入图片说明

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? Can I use some sort of array of NSArrayControllers to populate my NSOutlineView? Is there a way I can override NSTreeController to take in data from three sources?

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 :

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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