What's the most efficient way to expand all objects of a specific class in a NSOutlineView
? (The datasource of my outline view is a NSTreeController
).
Let's say I have
classA
classA
- classA
- classC
- classC
- classB
- classC
- classC
classB
- classC
I want to expand classA objects only. Do I need to iterate through the entire tree checking which class belong each object?
UPDATE Sorry I have to make a correction. The outlineView objects are NSTreeNodes from NSTreeController data source. And only the "representedObject" are those my custom classes.
So the structure with those classes is correct, but they are not directly accessible as nodes of the outline view.
For what it's worth, the important "glue" I mentioned in the comment is more or less straight from this great blog post: Saving the expand state of a NSOutlineView.
I took all the classes in my tree, and made them subclasses of an abstract class with an expanded
property, so every representedObject
in an NSTreeNode
has expanded
as a property.
But you may not even need to do that if you don't care about persisting expanded
in your data model.
The straightforward thing to do is to just iterate the rows:
for (NSUInteger row = 0; row < [outlineView numberOfRows]; row++)
{
// Expand item if it's an classA
NSTreeNode* treeNode = [outlineView itemAtRow:row];
if ([treeNode.representedObject isKindOfClass:[ClassA class]])
[sender.animator expandItem:treeNode];
}
... you'll notice that for loop borrows a lot of structure from the referenced blog post.
So I guess my answer is a lazy, "yes, iterate the whole tree." At least the displayed tree.
EDIT: For those who are a bit overzealous about MVC, I now feel it's necessary to specify that the above code should be in the class you use as the NSOutlineView
controller, which would usually implement <NSOutlineViewDelegate>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With