Hypothetical NSTreeController and NSOutlineView with bindings
Hypothetical NSTreeController and NSOutlineView with bindings
- Subject: Hypothetical NSTreeController and NSOutlineView with bindings
- From: René Puls <email@hidden>
- Date: Sun, 30 Nov 2003 13:12:40 +0100
Hello everyone,
this is going to be a longer post. In short: I have a proof-of-concept
implementation for an "NSTreeController", but not the slightest idea
how to subclass NSOutlineView. :-)
I am currently writing an application which uses the new controller
layer. Now I have finally run into the problem that I really *need* an
NSOutlineView to display some hierarchical data, but of course that
control does not yet have bindings support.
Currently I have some adaptor code in place which keeps the outline
view in sync with the model, but that part is quickly becoming a huge
mess compared to the rest of my application. I would rather spend some
time making NSOutlineView bindings-aware, if that is possible.
First of all, of course, an appropriate controller class is needed.
What I am currently using is a kind of "tree controller" which contains
a list of NSArrayControllers, one for each node in the tree.
Let's say I have a tree like this:
+ [root]
|
+---+ [node1] first top-level node
| |
| +---+ [node1-1] sub-node of node1
| |
| +---+ [node1-2] another sub-node of node1
|
+---+ [node2] second top-level node
Each node in this tree has an identifier (in square brackets). My tree
controller has a dictionary ("nodeDict") which maps identifiers to the
corresponding array controllers.
This is how the nodeDict would look for the tree above:
root => (node1, node2)
node1 => (node1-1, node1-2)
(There is no entry for "node2", as it does not yet have any sub-nodes.)
My tree controller has a "sortDescriptors" property, and the
"sortDescriptors" of every node array controller is bound to it. This
way, when the sortDescriptors of my tree controller change, it
automatically sorts all the subnodes--quite elegant.
But now to the ugly part. To connect this to an NSOutlineView, I have
these methods on the tree controller:
- (id)childAtIndex:(unsigned int)index ofItem:(id)item
{
NSArrayController *nodeArrayController = [nodeArrayControllers
objectForKey:[item nodeIdentifier]];
return [[nodeArrayController arrangedObjects] objectAtIndex:index];
}
- (unsigned int)numberOfChildrenOfItem:(id)item
{
NSArrayController *nodeArrayController = [nodeArrayControllers
objectForKey:[item nodeIdentifier]];
return [[nodeArrayController arrangedObjects] count];
}
(Yes, this currently depends on the items having a method called
"nodeIdentifier". There is probably a better way to do it--this is just
a proof-of-concept.)
My window controller, which is the data source for the outline view,
currently calls these two methods when it receives the
outlineView:child:ofItem: and outlineView:numberOfChildrenOfItem:
messages. This works quite well so far, but I would like to get rid of
the glue code for the data source and bind the NSOutlineView directly
to the tree controller.
Here is the problem: It seems easy to subclass
NSTableView/NSOutlineView when you just want to change its appearance;
but in my case I need to change the way NSOutlineView retrieves its
data. The documentation does not list any public methods at all for
this purpose (besides dataSource and setDataSource:).
So, has anyone ever subclassed NSTableView or NSOutlineView to change
its data-loading behavior? Or do I have to start from scratch and write
my own outline view class? That would be horrible.
Also, I would like to hear comments on the tree controller design. Is
this a good idea? Are there better ways to represent a tree and make it
work with the controller layer? Sorting is one issue that seems to work
quite well with this approach. Of course there is still the problem of
making the actual tree accessible via key-value coding. Currently, one
would have to use the node identifiers as key paths like
"treeController.nodeDict.root.arrangedObjects". I am still not quite
sure how this should be done...
Kind regards,
Rene Puls
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.