View Based NSTableView with Sections

If you want sections you basically have to roll your own (recognize that row x is supposed to be a section cell and provide a section view. TwUI has TUITableView which enables this (and massively improves scroll performance, in my experience).


I see this is an old question, but the answer is to use a View based NSTableView then implement tableView:viewForTableColumn:row:.

This is code based on how I do it. It hasn't been compiled in Xcode.

-(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    NSTableCellView *cell = nil;
        // get your row from your array of objects.
        // determine if it's a section heading or not.

    SomeClass *someObject = [self.myObjects objectAtIndex:row];

    if (someObject.isSectionHeading) {
        cell = [tableView makeViewWithIdentifier:@"HeaderCell" owner:self];
        cell.textField.objectValue = someObject.headingName;
    } else {
        cell = [tableView makeViewWithIdentifier:@"DataCell" owner:self];
        cell.textField.objectValue = someObject.rowValue;
    }

    return cell;

}

And also tableView:isGroupRow will put a grey background on your section headings

-(BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row {
    BOOL isGroup = NO;
    // Choose some way to set isGroup to YES or NO depending on your records.
    return isGroup;
}

Make sure in Interface Builder you have set the identifiers for your NSTableCellViews to "HeaderCell" and "DataCell". Or use whatever names you want. As long as it matches your code. You can have as many of these cells as you want.

If you make a subclass of NSTableCellView then you can easily add your own text fields, checkboxes, images, etc. to the view and set their values accordingly.


There is a very good and simple tutorial showing how to implement a NSTableView with sections with sample code on github. Just watch it here and in the video description there is a link to download the code.