Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View Based NSTableView with Sections

I am looking for a way to create the iOS like sections in NSTableView (like in iTunes 11 - Attached).

As you can see in the screenshot, "Albums" is one section and "Songs" is second. Any help would be appreciated.

Thanks!

enter image description here

like image 222
user754905 Avatar asked Dec 07 '12 18:12

user754905


1 Answers

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.

like image 146
Tap Forms Avatar answered Oct 23 '22 19:10

Tap Forms