Search code examples
objective-ccocoansoutlineviewnstreecontroller

Adding a child object using NSTreeController/NSOutlineView


In my application, like many Mac applications, I have a source list. At the moment this is an NSOutlineView bound to an NSTreeController. I can add items to it pretty easily, and have even been able to duplicate the "source list" appearance, with grey all-caps headers and all. There's something that evades me, though, and it's driving me bonkers.

How does one go about adding a child to a specific item in the tree? For instance, let's say I've created an item titled "Cheese". I've already added two children to Cheese, called "Cheddar" and "Swiss". How can I add "Longhorn Colby" to the list of children now that my initial adding method has finished running?

I've googled it, but can't find a simple, straight answer. From what I can see, though, it's needlessly complicated and akin to requiring an act of congress to do something as simple as eating breakfast. Please correct me if I'm wrong.

Update:

I don't believe I'm using subtypes. Up until now, here's how I've been populating my tree:

[treeController addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                               [NSNumber numberWithBool:YES], @"isSourceGroup",
                               @"CHEESE", @"name",
                               [NSArray arrayWithObjects:
                                [NSDictionary dictionaryWithObjectsAndKeys:
                                 @"Cheddar", @"name",
                                 nil],
                                [NSDictionary dictionaryWithObjectsAndKeys:
                                 @"Swiss", @"name",
                                 nil],
                                nil], @"children",
                               nil]];

The "isSourceGroup" bit is used to denote that this item is one of the headers in the outline view.


Solution

  • Try changing that NSArray to NSMutableArray, and then:

    [treeController insertObject:longhornColbyDict atArrangedObjectIndexPath:[[NSIndexPath indexPathWithIndex:0] indexPathByAddingIndex:0]];