Johnny Johnny - 2 months ago 62
Objective-C Question

NSTableRowView/NSTableCellView how to set custom color to selected row?

I am trying to implement custom row color when table row is selected.

-(void)tableViewSelectionDidChange:(NSNotification *)notification{


NSInteger selectedRow = [_mainTable selectedRow];

NSTableCellView *cell = [_mainTable rowViewAtRow:selectedRow makeIfNecessary:NO];

cell.layer.backgroundColor = [NSColor redColor].CGColor;

NSLog(@"selected");
}


But this is not working. I find that Apple documentation very confusing (maybe I am wrong). I am not experienced with Mac programming.

Can someone suggest any solution? Basically I need that selection Color to be transparent.

Answer

first set tableview selection highlight style to

 NSTableViewSelectionHighlightStyleNone

then in your tablView delegate implement

tableView:shouldSelectRow:

and write this code inside it:

UITableViewRow *row= [_mainTable rowViewAtRow:selectedRow makeIfNecessary:NO];
row.backgroundColor = [your color];
return YES;

read these also https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSTableViewDelegate_Protocol/index.html#//apple_ref/occ/intfm/NSTableViewDelegate/tableView:rowViewForRow:

for selection style https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTableView_Class/index.html#//apple_ref/occ/instp/NSTableView/selectionHighlightStyle