开发者

NSOutlineView -- combining multiple sources

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开发者_如何学Python 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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜