If you have a consistently constructed NSDictionary, then maybe, but it's probably not worth it. If you already have your data in a parent/children structure, it should be fairly easy.
In this example, a TreeNode object has an NSArray of children, with each object in that array being another TreeNode. TreeNode has a couple of convenience mehods -childAtIndex: and -numberOfChildren,
which are just covers for NSArray's -objectAtIndex: and -count methods.
The main thing to realize is that NSOutlineView passes back the parent "item" it needs information for. If the item is nil, then it's looking for info on the top-level node.
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">/******************************************
** Outline View DATA SOURCE METHODS
*******************************************/
// This method just returns back the appropriate TreeNode
// object for an "item" dataSource parameter. If the item
// is nil, then we return the root node (controllerTree),
// which is an ivar of this controller class. Otherwise,
// just use the item itself, which is a TreeNode object.
// I believe the examples in /Developer/Examples/AppKit/OutlineView
// do this with a macro, which is a little bit faster but
// may be more confusing when you're trying to learn this stuff.
- (TreeNode *)_nodeForItem
id)outlineItem
{
return outlineItem == nil? controllerTree : (BFTreeNode *)outlineItem;
}
// return the specified child of a node. When the outline
// view wants children of that child, then the object returned
// here will be the "item" in a future call of this method.
- (id)outlineView
NSOutlineView *)outlineView child
int)anIndex ofItem
id)item
{
return [[self _nodeForItem:item] childAtIndex:anIndex];
}
- (BOOL)outlineView
NSOutlineView *)outlineView isItemExpandable
id)item
{
return ([[self _nodeForItem:item] numberOfChildren] > 0);
}
- (int)outlineView
NSOutlineView *)outlineView numberOfChildrenOfItem
id)item
{
return [[self _nodeForItem:item] numberOfChildren];
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Now, if you have things structured as NSDictionary objects that always
have a "children" key, which points to an NSArray of more of these
NSDictionary objects, then the "items" are the NSDictionary objects,
and you do things like [[item objectForKey:@"children"] count] and
[[item objectForKey:@"children"] objectAtIndex:theIndex].
Just remember if the "item" parameter is nil,
then you want to return the top-level information.