Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View-based NSTableView / NSOutlineView and isGroupItem

I have a View-based NSOutlineView bound to a NSTreeController. Everything seems to work correctly until I implement the outlineView:isGroupItem: method in my delegate, then the group header suddenly stopped showing up. Like this

enter image description here

I confirmed that If I were to change the NSOutlineView to cell-based then the group item shows up properly. Similar behavior is also observed for NSTableView. Has anybody else encountered this problem?

like image 314
Tony Avatar asked Jan 01 '12 22:01

Tony


1 Answers

Solved it!

Turned out I had to implement the following method in the NSOutlineView delegate

- (NSView *)outlineView:(NSOutlineView *)outlineView 
     viewForTableColumn:(NSTableColumn *)tableColumn
                   item:(id)item {
    if ([self outlineView:outlineView isGroupItem:item]) {
        NSString *vId = [[[outlineView tableColumns] objectAtIndex:0] identifier];
        return [outlineView makeViewWithIdentifier:vId owner:self];
    }
    return [outlineView makeViewWithIdentifier:[tableColumn identifier] owner:self];
}

Apparently, by default view based NSOutlineView generate view for each cell in the table by locating the view with the same identifier as the column. In the case of a group item / group row however, there is no tableColumm associated with that row, therefore the view turns out to be nil and not show up.

Playing around with apple's TableViewPlayground sample project really helped! Highly recommended!

Addendum: In Interface Builder if you add Source List object instead of Outline View, it will add two prototype view rows for you.

Allowing you to design different looking header and data rows and referring to them in code by their identifier.

Header Cell: Interface Builder > Header Cell Data Cell: Interface Builder > Data Cell

You can of course also manually add as many prototype view rows as you like.

like image 75
Tony Avatar answered Sep 30 '22 13:09

Tony