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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With