Search code examples
objective-ccocoansoutlineviewnstreecontroller

Heterogeneous NSTreeController


I have an NSTreeController (supplying content to an NSOutlineView). I'd like the top-level objects to be of one class, and all other objects (so, children at any level) to be of another. What's the best way to go about this?

I'll need to somehow change the behavior of at least add, addChild, insert, and insertChild, I suppose. I was hoping, though, to find a simple way to account for this in only one location, rather than changing four separate methods.


Solution

  • This worked, and I didn't have to rewrite any functionality:

    - (void)insertChild:(id)sender
    {
        if ([self selectionIndexPath])
        {
            [self setObjectClass:[IRGroup class]];
            [super insertChild:sender];
        }
        else
        {
            [self setObjectClass:[IRFloor class]];
            [super insertChild:sender];
        }
    }
    

    It wasn't easy; I tried overriding newObject, because Apple's docs claim it is called when inserting siblings and children, but my testing reveals it is only called when inserting siblings.