Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSOutlineView : Expand all objects of a specific class only

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.

like image 694
aneuryzm Avatar asked Nov 01 '22 00:11

aneuryzm


1 Answers

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>

like image 164
stevesliva Avatar answered Nov 10 '22 14:11

stevesliva