1

我有一个由几个单元组成的自定义单元,其中一个是 NSPopUpButtonCell。

当突出显示我的自定义单元格时,我想让所有子单元格也突出显示(通常通过变白)。

例如,使用 NSTextCell,如果我在调用setHighlighted:YES之前调用drawWithFrame:inView该单元格,将使用白色文本绘制,完全符合我的要求。

这不适用于 NSPopUpButtonCells。文本只是继续绘制为黑色。

似乎这应该是可能的,因为将 NSPopUpButtonCell 正确地放入 NSTableView 突出显示。

有人可以指出我解决这个问题的正确方向吗?

4

1 回答 1

1

你在哪里托管这个自定义+复合 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 中托管此复合单元格,则足以获得所选行的正确背景。

如果您在自己的视图中托管它,您可能需要做额外的工作。(并且需要提供有关主机环境的其他详细信息,然后我们才能提供建议。)

于 2009-07-24T20:18:28.777 回答