简体   繁体   English

NSOutlineView 和 Core Data 1 → 许多关系

[英]NSOutlineView and Core Data 1 → Many Relationships

I have a Core Data model that supports a 1 → Many relationship (1 Folder to many Phrases).我有一个支持 1 → Many 关系的 Core Data 模型(1 个文件夹到多个短语)。 At the moment I'm just displaying the Phrases on a flat NSTableView using Core Data Bindings to a NSArrayController to glue everything together - this is working happily.目前我只是在平面NSTableView上显示短语,使用核心数据绑定到NSArrayController将所有内容粘合在一起 - 这工作很愉快。

I'm trying to experiment with an NSOutlineView to achieve the same result but showing the folders as well.我正在尝试使用NSOutlineView来实现相同的结果,但同时显示文件夹。 I've tried a similar binding structure that I'm already using with the NSTableView but I'm not making any headway.我已经尝试了一个类似的绑定结构,我已经在NSTableView使用了它,但我没有取得任何进展。 What steps should I take to move from a flat NSTableView to a NSOutlineView with 'depth'?我应该采取哪些步骤从平面NSTableView移动到具有“深度”的NSOutlineView

Here's my MOM:这是我的妈妈: 管理对象模型。我们在新西兰拼写母亲“妈妈”,所以没有想象中的那么好笑。

Top level folders, bottom level phrases.顶层文件夹,底层短语。 No nesting.没有嵌套。 I'm not an animal.我不是动物。

NSTreeController NSTreeController

  • I have a NSTreeController bound to the array controller for arrangedObjects on Controller Content我有一个NSTreeController绑定到数组控制器,用于Controller Content arrangedObjects
  • It is set to 'Entity Name' mode with an entity name of Folder (this feels wrong)设置为'Entity Name'模式,实体名称为Folder (这个感觉不对)
  • phrases is set to the children key path phrases设置为子键路径
  • Prepares Content is true准备内容为真
  • It's mOC is set to a valid mOC它的 mOC 设置为有效的 mOC

NSOutlineView大纲视图

  • Outline View Content is bound to the Tree Controller using arrangedObjects as the Key and string as the Key Path Outline View Content 绑定到Tree Controller,使用arrangementObjects 作为Key,字符串作为Key Path

The Phrase NSManagedObject短语 NSManagedObject

  • has an extension called 'phrases' which returns an empty set, since it'll never have children.有一个名为 'phrases' 的扩展名,它返回一个空集,因为它永远不会有孩子。

When I do this, I get this as a result:当我这样做时,我得到了这个结果:

[APPNAME.Folder copyWithZone:]: [APPNAME.Folder copyWithZone:]:

The tutorial that got me this far had nested objects and only one type of entity, where as I have two. 让我走到这一步教程有嵌套对象和只有一种类型的实体,而我有两种。 What do I need to have an accurate representation of my core data objects in a NSOutlineView ?我需要什么才能在NSOutlineView准确表示我的核心数据对象?

NSTreeController and NSOutlineView work in the same way as NSTableView and NSArrayController . NSTreeControllerNSOutlineView工作方式与NSTableViewNSArrayController相同。 To use NSTreeController you don't need a NSArrayController , you can put the treecontroller in entity mode.要使用NSTreeController您不需要NSArrayController ,您可以将树控制器置于实体模式。

my entities are not called Table View Cell我的实体不称为表视图单元格

Remove the column binding, it is for cell based ouline views.删除列绑定,它用于基于单元格的 ouline 视图。 Bind the content of the outline view to the tree controller and bind the text fields in the Table View Cells to the Table Cell View.将大纲视图的内容绑定到树控制器,并将表视图单元格中的文本字段绑定到表单元格视图。

The tutorial that got me this far had nested objects and only one type of entity, where as I have two.让我走到这一步的教程有嵌套对象和只有一种类型的实体,而我有两种。

This is a problem because NSTreeController has only one childrenKeyPath and the textfield in the outline view can only bind with one key.这是一个问题,因为NSTreeController只有一个childrenKeyPath ,而大纲视图中的 textfield 只能绑定一个键。 As far as I know there are two workarounds.据我所知,有两种解决方法。

Solution 1: Create a subclass of NSTreeController and override方案一:创建NSTreeController的子类并覆盖

- (NSString *)childrenKeyPathForNode:(NSTreeNode *)node

the managed object is node.representedObject .托管对象是node.representedObject Implement the delegate method outlineView:viewForTableColumn:item: and use a different view for each entity.实现委托方法outlineView:viewForTableColumn:item:并为每个实体使用不同的视图。

Solution 2: Create NSManagedObject subclasses and implement calculated properties for children and display so all entities answer to the same keys.解决方案 2:创建NSManagedObject子类并为子项实现计算属性并显示,以便所有实体都回答相同的键。 I think this solution feels wrong, managed objects shouldn't contain code for views, but it is very easy to implement if you already have NSManagedObject subclasses.我认为这个解决方案感觉不对,托管对象不应该包含视图的代码,但是如果您已经拥有NSManagedObject子类,那么它很容易实现。

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

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