2
votes

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.

2
If what you need to say can't be contained in the comment form, there's a very good chance it should just be made an edit to your question for clarification. I went ahead and merged your answer into your question. – Tim Post

2 Answers

2
votes

Try changing that NSArray to NSMutableArray, and then:

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

Assuming every TypeOfFood has a subtypes property holding an array, and that that property is set in the tree controller as the nodes' children key path, send the Cheese type-of-food an insertObject:inSubtypesAtIndex: message. (You'll need to have implemented that accessor in the TypeOfFood class.)

Since this is a KVC-compliant accessor, KVO will wrap it when the tree controller starts observing the property, which means that sending the accessor message should be enough to trip the right notifications and cause the right side effects—namely, updating the controller and the view.

I haven't used NSTreeController, though, so it's entirely possible that I've missed some aspect of it that makes this answer bogus. Please try it and comment with whatever happens.