Expand only root node in NSOutlineView

715 views Asked by At

I am trying to solve a simple problem of expanding only the root item in my NSOutlineView but with no luck. I can expand all items and selected items but I seem unable to figure out how to identify exactly how to expand just the root node. The outlineview shows a tree of folders and i want to show the folders available just under root folder, but I do not want to expand the folders children.

I currently try to set the selectionIndexPath of my NSTreeController and then expand the selection in outlineview:

 NSIndexPath *indexPath;
 NSUInteger section = [indexPath indexAtPosition:0];
 NSIndexPath *ip = [NSIndexPath indexPathWithIndex:section];
 [outlineView collapseItem:nil collapseChildren:YES];

 [self.treeController setSelectionIndexPath:ip];
 [outlineView expandItem:[self.treeController selectionIndexPath]];

This does not work. Suggestions for how I can correctly solve this is greatly appreciated.

Cheers, Trond

Solution: The following combination of what I already had and @PaulPatterson suggestion works perfectly:

[outlineView collapseItem:nil collapseChildren:YES];
NSIndexPath *indexPath;
NSUInteger section = [indexPath indexAtPosition:0];
NSIndexPath *ip = [NSIndexPath indexPathWithIndex:section];
[outlineView collapseItem:nil collapseChildren:YES];

[self.treeController setSelectionIndexPath:ip];
id node = [[self.treeController selectedNodes] firstObject];
[outlineView expandItem:node];
1

There are 1 answers

1
Paul Patterson On BEST ANSWER

Is the problem a mismatch between the the value expected by [-NSOutlineView expandItem:] and the value returned by [-NSTreeController selectionIndexPath]?

As it stands your passing am NSIndexPath instance to expandItem:. I appreciate that expandItem: is agnostic about the specific class that it expects as it's argument (the docs state it accepts any object - id), but my experience is that it wants a node object - the type of object contained in the array returned by [-NSTreeController selectedNodes]. Try the following:

if ([[self.treeController selectedNodes] count] == 1) {
    id node = [[self.treeController selectedNodes] firstObject];
    [outlineView expandItem:node];
}