How can I change the color of a cell in my NSTableView?
4 Answers
In your NSTableViewDelegate
for the NSTableView
, implement this method:
- (void)tableView:(NSTableView *)tableView
willDisplayCell:(id)cell
forTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
The NSTableView
calls this on its delegate before displaying each cell so that you can affect its appearance. Assuming you're using NSTextFieldCells, for the cell that you want to change call:
[cell setBackgroundColor:...];
Or, if you want to change the text color:
[cell setTextColor:...];
If you want columns to have different appearances, or if all of the columns aren't NSTextFieldCells, use [tableColumn identifier]
to, er, identify the column. You can set the identifier in Interface Builder by selecting the table column.

- 2,162
- 17
- 28
-
11Make sure `drawsBackground` is enabled on the NSTextFieldCell otherwise this will have no effect! – samvermette Feb 04 '12 at 01:22
-
1This only works for NSCell based tables, not NSView ones @MiguelCarvajal – Ron Davis Jun 21 '18 at 15:06
// TESTED - Swift 3 solution...for changing color of cell text in a single column. All columns in my tableview have a unique identifier
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let myCell:NSTableCellView = tableView.make(withIdentifier: (tableColumn?.identifier)!, owner: self) as! NSTableCellView
if tableColumn?.identifier == "MyColumn" {
let results = arrayController.arrangedObjects as! [ProjectData]
let result = results[row]
if result.ebit < 0.0 {
myCell.textField?.textColor = NSColor.red
} else {
myCell.textField?.textColor = NSColor.black
}
}
return myCell
}

- 165
- 1
- 4
-
I am trying to use this method to change the background color of the ```cellView.textField?.backgroundColor = NSColor.yellow``` but it doesn't work. How would I change your answer to make this work for the background color? I also tried applying a backgroundFilter to the cellView itself ```cellView.backgroundFilters.append(highlightFilter)``` but that only changes the text color to yellow. My filter is set as ```let highlightFilter = CIFilter(name: "CIFalseColor")!``` then ```let highlightCIColor = CIColor(red: 0.9608, green: 0.9765, blue: 0, alpha: 1.0)``` (continued)... – SouthernYankee65 Feb 19 '21 at 21:16
-
and finally in ```viewDidLoad()``` I set the filter ```highlightFilter.setValue(highlightCIColor, forKey: "inputColor1")```. But as I wrote in my previous comment this only turns the text color yellow and not the background of the cellView. I want to be able to change the individual cell background. Changing the textColor works fine, as expected. – SouthernYankee65 Feb 19 '21 at 21:18
-
this is technically wrong, because it is for View based Tables, not for Cell based! – Ol Sen Nov 20 '21 at 16:03
//for VIEW based TableViews using Objective C
//in your NSTableViewDelegate, implement the following
//this customization makes the column numbers red if negative.
- (NSView *)tableView:(NSTableView *)inTableView
viewForTableColumn:(NSTableColumn *)tableColumn
row:(NSInteger)row
{
NSTableCellView *result = nil;
if ([tableColumn.title isEqualToString: @"Amount"]) {
//pick one of the following methods to identify the NSTableCellView
//in .xib file creation, leave identifier "blank" (default)
result = [inTableView makeViewWithIdentifier:[tableColumn identifier] owner:self];
//or set the Amount column's NSTableCellView's identifier to "Amount"
result = [inTableView makeViewWithIdentifier:@"Amount" owner:self];
id aRecord = [[arrayController arrangedObjects] objectAtIndex:row];
//test the relevant field's value
if ( aRecord.amount < 0.0 )
[[result textField] setTextColor:[NSColor colorWithSRGBRed:1.0 green:0.0 blue:0.0 alpha:1.0]];
} else {
//allow the defaults to handle the rest of the columns
result = [inTableView makeViewWithIdentifier:[tableColumn identifier] owner:self];
}
return result;
}

- 11
- 2
Try using a custom NSView
for that, or NSTableView
's -setBackgroundColor:
method.

- 15,967
- 12
- 58
- 83
-
6NSTableView setBackgroundColor accomplishes nothing with regards to setting the color of the NSTableViewCell. – Jason Fuerstenberg Oct 07 '12 at 04:45
-
1This is a completely incorrect answer. It doesn't answer the original question. – Oliver Cooper Feb 13 '15 at 10:09