在选择时更改NSTextFieldCell背景颜色

Don*_*van 1 cocoa nstextfieldcell

我正在尝试在选择单元格时更改NSTextFieldCell的背景颜色.

这是代码:

- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {

    [super drawWithFrame:cellFrame inView:controlView];

    if([self isHighlighted]) {
        [self setBackgroundColor:[NSColor whiteColor]]; 
    }   
}
Run Code Online (Sandbox Code Playgroud)

但所选行始终为蓝色.我错过了什么?

注意:这不是iOS应用程序.

提前谢谢你.

Ale*_*lex 5

这并不像我之前想的那样容易:NStableview存在一些问题.如果你使用类似的东西:

[destinationsListForSaleTableView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
Run Code Online (Sandbox Code Playgroud)

你必须做

 - (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex
{
 if ([[aTableView selectedRowIndexes] containsIndex:rowIndex]) {
    [aCell setBackgroundColor: [NSColor colorWithDeviceRed:0.29 green:0.27 blue:0.42 alpha:1]];

} else [aCell setBackgroundColor: [NSColor colorWithDeviceRed:0.52 green:0.54 blue:0.70 alpha:1]];
[aCell setDrawsBackground:YES];

}  
Run Code Online (Sandbox Code Playgroud)

如果你的单元格不是自定义的,那就足够了.如果你改变高度和内部,它必须更复杂:

保留选择样式表供您选择.在细胞亚类中:

-(NSColor *)highlightColorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView {
return nil;
//[NSColor colorWithDeviceRed:0.29 green:0.27 blue:0.42 alpha:1];
}

 - (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
if ([self isHighlighted]) {
    [[NSColor colorWithDeviceRed:0.29 green:0.27 blue:0.42 alpha:1] set];
    cellFrame.origin.x -= 1;
    cellFrame.origin.y -= 1;
    cellFrame.size.height += 2;
    cellFrame.size.width += 3;

    NSRectFill(cellFrame);

}
[super drawWithFrame:cellFrame inView:controlView];
}
Run Code Online (Sandbox Code Playgroud)

你问我为什么要改变灌装的大小?Bcs当你使用背景时,苹果留下一个小盒子,它会有不同的颜色.