NSOutlineView——组合多个源

发布于 2024-10-02 03:14:05 字数 259 浏览 10 评论 0 原文

在我的应用程序中,我有一个 NSOutlineView,它从 NSTreeController 获取数据,而 NSTreeController 又从核心数据模型获取数据。

我现在想做的是将组标题添加到大纲视图中,也许还添加一些附加行——显然这些东西应该存在于模型之外并成为视图的一部分。但是,尽管我对此绞尽脑汁,但我想不出任何方法可以让大纲视图在不修改底层模型的情况下显示这些内容,这显然是一个很大的禁忌。

非常感谢您的帮助。我觉得我在这里错过了一些明显的东西......

In my app, I have an NSOutlineView that gets its data from a NSTreeController -- which in turn gets it from the Core Data model.

What I would like to do now is to add group headings and maybe some additional rows to the outline view -- obviously things that should exist outside of the model and be part of the view. But, as much as I scratch my head over this, I can't think of any way to make the outline view display these things without modifying the underlying model, which is obviously a big no-no.

Your help is very appreciated. I feel like I am missing something obvious here...

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

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

发布评论

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

评论(1

榕城若虚 2024-10-09 03:14:05

您在这里要做的是编写一个自定义的 NSTreeController 子类。这就是为什么这是您想要进行更改的完美位置的原因:

  • 正如您所说,它不在模型中。
  • 与视图无关——完全透明。
  • 基本上,您想要的是从保存的数据中创建显示的数据<-这是控制器的任务。

幸运的是,Cocoa 中的控制器类非常强大,同时也非常简单。对你来说,覆盖 -arrangedObjects 方法。重用默认实现,因为它做了很多有用的事情,例如应用谓词或排序。其外观如下:

- (id)arrangedObjects {
  id root = [super arrangedObjects];

  // "root" is something like a NSTreeNode but not quite yet
  NSTreeNode *newRoot = [NSTreeNode treeNodeWithRepresentedObject: nil];
  [[newRoot mutableChildNodes] setArray: [root childNodes]];

  // Do your customizations here

  return newRoot;
}

返回的对象属于类 NSTreeNode - 请参阅有关如何进行修改的文档。

What you would do here is to write a custom NSTreeController subclass. Here is why this is the perfect place for the changes you want to do:

  • It's not in the model, as you said.
  • Has nothing to do with the view -- completely transparent.
  • Basically what you want is to create displayed data out of saved data <- this is a controller's task.

Luckily, the Controller classes in Cocoa are very powerful and very simple at the same this. For you it should be enough to override the -arrangedObjects method. Re-use the default implementation, as it does a lot of useful things like applying predicates or sorting. Here's how this could look like:

- (id)arrangedObjects {
  id root = [super arrangedObjects];

  // "root" is something like a NSTreeNode but not quite yet
  NSTreeNode *newRoot = [NSTreeNode treeNodeWithRepresentedObject: nil];
  [[newRoot mutableChildNodes] setArray: [root childNodes]];

  // Do your customizations here

  return newRoot;
}

The returned object is of the class NSTreeNode - see the documentation on how to do modifications.

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