你在哪里托管这个自定义+复合 NSCell 子类?
-setHighlighted:YES 不是你要找的。从文档中:
默认情况下,此方法不执行任何操作。NSButtonCell 类重写此方法以绘制具有由 NSCellLightsByBackground、NSCellLightsByContents 或 NSCellLightsByGray 指定的外观的按钮。
通常,单元格的宿主视图将设置单元格的背景样式,并且单元格将在绘制时使用它来适当地显示自己。将背景样式从主单元格传播到子单元格。
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
{
NSRect textRect, popUpRect;
NSDivideRect(cellFrame, &textRect, &popUpRect, NSWidth(cellFrame) / 2, NSMinXEdge);
/* Draw the text cell (self) */
[super drawInteriorWithFrame: textRect inView: controlView];
/* Draw our compound popup cell - create & release every time drawn only in example */
NSPopUpButtonCell *popUpCell = [[NSPopUpButtonCell alloc] initTextCell: @"popup title"];
[popUpCell setBordered: NO];
[popUpCell setBackgroundStyle: [self backgroundStyle]];
[popUpCell drawWithFrame: popUpRect inView: controlView];
[popUpCell release];
}
如果您在 NSTableView 中托管此复合单元格,则足以获得所选行的正确背景。
如果您在自己的视图中托管它,您可能需要做额外的工作。(并且需要提供有关主机环境的其他详细信息,然后我们才能提供建议。)