Change color of NSTableViewCell
Asked Answered
B

4

11

How can I change the color of a cell in my NSTableView?

Barger answered 21/5, 2010 at 2:2 Comment(0)
A
0

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

Athey answered 21/5, 2010 at 2:5 Comment(2)
NSTableView setBackgroundColor accomplishes nothing with regards to setting the color of the NSTableViewCell.Millner
This is a completely incorrect answer. It doesn't answer the original question.Phylum
M
14

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.

Meteorograph answered 22/5, 2010 at 1:29 Comment(2)
Make sure drawsBackground is enabled on the NSTextFieldCell otherwise this will have no effect!Ingressive
This only works for NSCell based tables, not NSView ones @MiguelCarvajalPremise
A
2

// 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
    }
Archaeological answered 18/3, 2017 at 22:3 Comment(3)
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)...Pacer
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.Pacer
this is technically wrong, because it is for View based Tables, not for Cell based!Colugo
O
1
//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;
}
Oocyte answered 18/5, 2018 at 11:51 Comment(0)
A
0

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

Athey answered 21/5, 2010 at 2:5 Comment(2)
NSTableView setBackgroundColor accomplishes nothing with regards to setting the color of the NSTableViewCell.Millner
This is a completely incorrect answer. It doesn't answer the original question.Phylum

© 2022 - 2024 — McMap. All rights reserved.