views:

53

answers:

2

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.

A: 

It seems to me that you could just create an attribute in your objects to differentiate which objects should use your modified methods and which don't. Then just put a simple if statement to test for that attribute in the subclassed methods. If your object doesn't have the attribute then let the super class tree controller handle it otherwise your changed behavior.

regulus6633
A: 

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.

andyvn22