6

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?

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Tony
  • 36,591
  • 10
  • 48
  • 83

1 Answers1

12

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.

catlan
  • 25,100
  • 8
  • 67
  • 78
Tony
  • 36,591
  • 10
  • 48
  • 83
  • Why isn't `outlineView:viewForTableColumn:item:` in the [official `NSOutlineViewDelegate` docs](https://developer.apple.com/library/mac/documentation/cocoa/reference/NSOutlineViewDelegate_Protocol/Reference/Reference.html) – zakdances Sep 12 '13 at 10:46
  • how to remove the grouped header separator lines (lines on top and bottom in groups header)? OR how can we customize grouped NSOutlineVIew header? – Amit Khandelwal Aug 06 '15 at 07:45