views:

76

answers:

1

I have a Mac OS X application that uses an NSOutlineView with two columns: key and value, where you can edit the value column. I either have a NSString or a NSDictionary in a row. The code for the value of the cells is like this:

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {

    if ([[[tableColumn headerCell] stringValue] isEqualToString:@"Key"]) {
        id parentItem = [outlineView parentForItem:item] ? [outlineView parentForItem:item] : root;

        return [[parentItem allKeysForObject:item] objectAtIndex:0];
    } else {
        if ([item isKindOfClass:[NSString class]]) {
            return item;
        } else if ([item isKindOfClass:[NSDictionary class]]) {
            return @"";
        } else {
            return nil;
        }
    }
}

It's working as it should, except for when to value fields has the same string value. It always just takes the first element with that value to show as the key, so the same key value will appear for all value values that are the same. Anybody know how to fix this problem?

+1  A: 

It looks like you're showing a tree of dictionaries, whose objects are either strings or dictionaries.

The first problem is that every item object must uniquely identify a row. Neither the key nor the value has this property. (The key would if this were a flat table view, but this is an outline view, and two dictionaries—one a descendant, sibling, or cousin of the other—can have the same key.) Instead, you should make a model object for each key-value pair.

Second, the dictionary-value rows should be group items. There's a delegate method you can implement for this.

Peter Hosey