方法一、
1. set tableview selection highlight style to NSTableViewSelectionHighlightStyleNone
2.in your tablView delegate implement tableView:shouldSelectRow: and write this code inside it
NSTableViewRow *row= [_mainTable rowViewAtRow:selectedRow makeIfNecessary:NO];
row.backgroundColor = [your color];
return YES;
方法二、
创建NSTableRowView的子类并在NSTableView的delegate方法中返回子类实例
-(NSTableRowView*)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
1.子类实例重写drawSelectionInRect方法,去改变选中后的背景色
@implementation MyTableRowView
//方式1. 单独改变选中后的背景色
- (void)drawSelectionInRect:(NSRect)dirtyRect
{
[super drawSelectionInRect:dirtyRect];
[[NSColor yellowColor] setFill];
NSRectFill(dirtyRect);
}
//方式2. 改变选中后的背景色和 分割线
- (void)drawSeparatorInRect:(NSRect)dirtyRect
{
// Change the separator color if the row is selected
if (self.isSelected) [[NSColor orangeColor] setFill];
else [[NSColor grayColor] setFill];
// Fill the seperator
dirtyRect.origin.y = dirtyRect.size.height - 1.0;
dirtyRect.size.height = 1.0;
NSRectFill(dirtyRect);
}
2. return your subclassed row view using the rowViewForRow
NSTableView
delegate method:
- (NSTableRowView*)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
static NSString* const kRowIdentifier = @"MyTableRow";
MyTableRowView* myRowView = [tableView makeViewWithIdentifier:kRowIdentifier owner:self];
if (!myRowView) {
myRowView = [[MyTableRowView alloc] initWithFrame:NSZeroRect];
myRowView.identifier = kRowIdentifier;
}
return rowView;
}
Resources
Overriding NSTableRowView display settings
https://developer.apple.com/reference/appkit/nstablerowview
NSTableview rowViewForRow delegate method
https://developer.apple.com/reference/appkit/nstableviewdelegate/1532417-tableview