Colorizing a string in an NSTableView

What I want to do is set the background color of the selected row to an NSTableView when I click. I have seen other cases where people used tableView:willDisplayCell:forTableColumn:row:and setBackgroundColor:, but I don’t think this will work in my situation when I want this to happen when the button is pressed.

I know that I can find the selected row with the NSTableView method selectedRowand set the background color for the cell with setBackgroundColor:, but what I don’t know how to do is get from NSInteger for the selected row in NSCell to set the background color.

+5
source share
3 answers

NSTableView NSCell . . - tableView:willDisplayCell:forTableColumn:row:.

, reloadDataForRowIndexes:columnIndexes:.

+7

NSTableview

  - (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell1 forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row 
{
      if(row==1)
         [cell1 setBackgroundColor:[NSColor redColor]];
      else if(row==2||row==3)
         [cell1 setBackgroundColor:[NSColor greenColor]];
      else
         [cell1 setBackgroundColor:[NSColor clearColor]];
}

, . , drawsBackground NSTextFieldCell, !

+6

:

NSInteger selectedRow = [self.nsTableView selectedRow];
NSTableRowView* rowView = [self.nsTableView rowViewAtRow:selectedRow makeIfNecessary:NO];
[rowView setBackgroundColor:[NSColor blackColor]];

glhf

+5

All Articles