Taylee
Taylee

Reputation: 3

How to set color of one row in NSTableView?

Who can help me? I want make the row to be red, but it is always all row to be red!

-(void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell
  forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
    NSColor *cRed = [NSColor redColor];
    NSTextFieldCell* cCell = (NSTextFieldCell*)cell;
    if ([[tableView selectedRowIndexes] containsIndex:4])
        {
            [cCell setTextColor:cRed];
        }
        else
        {
            [cCell setTextColor:[NSColor blackColor]];
        }
}

Upvotes: 0

Views: 145

Answers (1)

Michael Dautermann
Michael Dautermann

Reputation: 89509

If any of your selected rows in your table include the 5th row (i.e. index 4), this line in your code:

if ([[tableView selectedRowIndexes] containsIndex:4])

hits and you are seeing red in every row being displayed.

Perhaps if you change that line to:

if(row == 4)

(where row is a parameter passed into that method)

You'll just see one row changed to red.

To be honest, this isn't the best place to set a row/cell's color. I think it would be better to set it in your table view data source (e.g. tableView:objectValueForTableColumn:row:).

Upvotes: 1

Related Questions