2009-07-22 65 views
1

我有一個由幾個單元格組成的自定義單元格,其中一個是NSPopUpButtonCell。如何在繪製時手動突出顯示NSPopUpButtonCell(使用白色而不是黑色繪製)?

當繪製我的自定義單元格突出顯示時,我想要使所有子單元格也突出顯示(通常通過變成白色)。

對於例如NSTextCell,如果我在調用drawWithFrame:inView之前調用setHighlighted:YES,則單元格將以白色文本繪製,完全按照我的需要繪製。

這不適用於NSPopUpButtonCells。文本只是繼續畫成黑色。

看來這應該是可能的,因爲將NSPopUpButtonCell放入NSTableView中會正確突出顯示。

有人可以指出我正確的方向來解決這個問題嗎?

回答

1

你在哪裏託管這個定製+複合NSCell子類?

-setHighlighted:是不是你在找什麼。從文檔:

默認情況下,此方法什麼也不做。 的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中託管此複合單元,那應該足以獲得所選行的正確背景。

如果您以您自己的觀點託管此視圖,則可能需要執行其他工作。 (在提供建議之前,需要提供有關主機環境的其他詳細信息。)

+0

這正是我所需要的。謝謝吉姆。 – 2009-07-24 20:31:33