在同一个 NSOutlineView 中显示不同的 NSManagedObject 实体

发布于 2024-10-17 06:52:56 字数 623 浏览 5 评论 0原文

基本上我有三个不同的核心数据实体(ABC),A 包含一组 < code>B 和 B 包含一组 C。然而,这三个实体彼此之间有很大不同,它们没有通用的方法来访问彼此的子实体或要在视图中显示的值。

我试图在 NSOutlineView 中显示这三个实体。可能还有其他解决方案,但我提出了两种不同的解决方案:

  1. 实现 NSOutlineViewDataSource 协议并以不同方式处理每个实体。

  2. 将类与类别合并,并添加常见(瞬态)方法/属性以供 NSOutlineView 使用。这些方法/属性从模型实体的属性中获取实际值。

我选择了第二个选项,并为子项和显示值添加了吸气剂。然而,这样一来,键值观察就不再起作用,模型中的更改也不会反映到视图中。我明白为什么,但我不确定如何以正确的方式解决这个问题。我正在考虑某种方式来通知实际模型值的变化并将其转发到视图。

有没有简单的方法来转发这些通知,或者我应该考虑其他选择吗?

简而言之,我需要在 NSOutlineView 中显示不同类型的实体,并且我不想弄乱模型。

Basically I have three different Core Data entities (A, B, C) and A contains a set of Bs and B contains a set of Cs. These three entities are, however, quite different from each other and they don't have common methods to access each other's children or the values to be displayed in the view.

I'm trying to display these three enties in an NSOutlineView. There's propably other solutions too but I came up with two different ones:

  1. Implementing NSOutlineViewDataSource protocol and handling each of the entities differently.

  2. Consolidate the classes with categories and add common (transient) methods/properties for the NSOutlineView to use. These methods/properties get their actual values from the model entities' properties.

I chose the second option and added getters for the children and the display value. This way, however, the Key-Value Observing does not work anymore and changes in the model are not reflected to the view. I understand why, but I'm not sure how to solve this the right way. I was thinking of some way to be notified of the actual model value changes and forward them to the view.

Is there any easy way to forward those notifications or should I consider some other alternative?

In short, I need to display different kinds of entities in an NSOutlineView and I don't want to mess the model.

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

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

发布评论

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

评论(2

雪落纷纷 2024-10-24 06:52:56

难道你不能只使用关系A->B->C来获取对象A并在大纲视图中显示它们吗?

Can't you just use relationship A->B->C to fetch the object A and display them in outline view?

七月上 2024-10-24 06:52:56

经过一番尝试和错误后,我发现创建自定义数据源确实如此简单,并决定采用第一个选择。此外,通过拖放放弃支持,这感觉更自然。

唯一的问题是 outlineView:setObjectValue:forTableColumn:byItem: 需要指定列标识符引用。我觉得不直接的依赖始终是人们应该避免的事情,但与使这项工作顺利进行相比,这是一件小事。

After some trial and error, I found out that creating a custom data source is really that simple and decided to go with the first choice. Also, with drag & drop support this feels much more natural way.

The only issue was with outlineView:setObjectValue:forTableColumn:byItem: for which one needs to specify column identifier references. I feel that undirect dependencies are always something one should avoid, but this is a small matter in comparison to making this work nicely.

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