7

I am developing a MAC application and included the tableView. Want to change the Colour of selected row to yellow.

stevesliva
  • 5,351
  • 1
  • 16
  • 39
Raghav
  • 625
  • 1
  • 12
  • 31
  • This might help - http://stackoverflow.com/questions/7038709/change-highlighting-color-in-nstableview-in-cocoa – D.T Oct 24 '13 at 08:58

2 Answers2

14

Set this on your table view:

[yourtableview setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];

And implement the following delegate method of NSTableView as:

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
    if ([[aTableView selectedRowIndexes] containsIndex:rowIndex]) 
    {
       [aCell setBackgroundColor: [NSColor yellowColor]];   
    } 
    else 
    {
       [aCell setBackgroundColor: [NSColor whiteColor]];
    } 
    [aCell setDrawsBackground:YES];
}  
Neha
  • 1,751
  • 14
  • 36
  • 5
    Assuming you're talking about a cell-based table, this does not work for the same reason as the previous answer, unless your cell happens to have no padding whatsoever (and maybe not even then). To change the row highlight, override the table's highlightSelectionInClipRect: method and draw the background color yourself, as shown here: http://stackoverflow.com/questions/7038709/change-highlighting-color-in-nstableview-in-cocoa – dgatwood Apr 17 '14 at 20:02
0

If you want to heighlight only individual cell of column then implement like this below:-

- (void)tableView:(NSTableView *)tableView
  willDisplayCell:(id)cell
   forTableColumn:(NSTableColumn *)tableColumn
              row:(NSInteger)row
{
    if ([[tableColumn identifier] isEqualToString:@"yourColumm"])
    {
        [cell setBackgroundColor:[NSColor yelloColor]];
    }
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
  • The background of a cell in a selected row gets completely covered by the row highlight, so setting the background color of the cell doesn't change the cell's appearance at all, as far as I can tell. – dgatwood Apr 17 '14 at 19:34