Re: NSTreeController and NSOutlineView: How do I get to the outline view's item
Re: NSTreeController and NSOutlineView: How do I get to the outline view's item
- Subject: Re: NSTreeController and NSOutlineView: How do I get to the outline view's item
- From: Quincey Morris <email@hidden>
- Date: Wed, 16 Apr 2008 00:18:26 -0700
On Apr 15, 2008, at 19:48, Markus Spoettl wrote:
I have a NSDocument subclass with a simple tree structure attached
to an NSOutlineView via NSTreeController and bindings. I learned -
through this list - to add items in KVO compliant way like this
TreeNode *node = [[TreeNode init] alloc];
[[self mutableArrayValueForKeyPath:@"treeContent"] addObject:node];
this works very well the NSOutlineView gets updated and displays the
structure I fill into my model.
The only thing that I can't seem to figure out is how to get to the
NSOutlineView's item for a specific node that was added. Say I want
to expand or collapse a specific node when it's added:
[myOutlineView expandItem:node];
does not work - I believe because it's the model's data object not
the item that represents it in the OutlineView (right?).
So how do I determine the outline item for a data node?
AFAIK, there's no way to expand a row in the view starting from the
data model. (I mean, by doing something to the data model alone, and
having a KVO notification trigger expansion in the outline view.)
If you're doing this in the context of populating the data model (e.g.
creating nodes when initializing a new document), you'd probably want
to do it by brute force: create the model data, then examine suitable
rows of the outline view (possibly all of them), expanding the ones
that correspond to a model object that needs to be expanded. In your
scenario, the outline view items are NSTreeNode objects provided by
the tree controller, and the NSTreeNode's representedObject is your
model object -- a TreeNode, I guess.
If you're doing this in the context of a user interface action (e.g.
inside an action method that gets called when a button is clicked to
"add a something"), you might be able to take a lighter-weight
approach. If you can work out, directly, where the row(s) were added,
you can just expand the relevant row(s) after the data model has been
updated. In this case, it might be convenient to use tree controller
convenience methods such as add: or insert:, or addObject: or
insertObject:, to work with the view-side picture of the data, letting
the data model get updated as a side effect -- instead of using
mutableArrayValueForKey directly.
Someone may step in and correct me here, but I believe that
programatically controlling the expanded/contracted state of outline
view rows has always been a PITA.
HTH
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden