views:

50

answers:

2

Hey guys,

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.

A: 

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.

Peter Hosey
A: 

Well, I was going to comment your answer in order to answer you, Peter, but the comments don't seem to support multiple lines or code.

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.

iindigo