2012-02-28 27 views
1

我有一個包含我的自定義NSCell子類IconCell的NSTableView。NSButton在自定義NSCell中繪製,但實際上不可點擊

IconCell包含三個元素:圖像,文本和按鈕。

這裏是我的繪製代碼的簡化版本(closeButton是按鈕):

- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView 
{ 
    NSPoint cellPoint = cellFrame.origin; 

    [controlView lockFocus]; 

    CGFloat buttonWidth = [closeButton frame].size.width; 

    [someNSImage drawInRect:NSMakeRect(cellPoint.x, cellPoint.y, ICON_WIDTH, ICON_HEIGHT) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:YES hints:nil]; 
    [someNSString drawInRect:NSMakeRect(cellPoint.x+ICON_WIDTH+PADDING, cellPoint.y, cellFrame.size.width - ICON_WIDTH - buttonWidth, cellFrame.size.height) withAttributes:someTextAttributes]; 
    [(NSButtonCell*)[closeButton cell] drawWithFrame:NSMakeRect(cellPoint.x + cellFrame.size.width - buttonWidth, cellPoint.y, buttonWidth, cellFrame.size.height) inView:controlView]; 

    [controlView unlockFocus]; 
} 

拱起部工作正常,併產生類似如下:

cell screenshot

這是什麼我想要。此外,當用戶與單元交互時,我希望發生以下兩種情況之一:如果用戶單擊單元格上的任何位置,除了關閉按鈕,它應該執行actionA。如果用戶點擊關閉按鈕,它應該做actionB

我遇到的問題是關閉按鈕似乎是「不可見的」 - 如果我點擊它,它不會移動(而工作按鈕應該顯示其按下狀態),並且通常它的行爲好像它不在那裏,並且actionA被觸發而不是actionB

我這是怎麼設置的兩個動作:

[tableView setAction:@selector(actionA)]; 

[closeButton setAction:@selector(actionB)]; 

我在做什麼錯?

回答

1

你只是在繪製單元格中的按鈕圖片。這與將實際按鈕放入單元格不同。

單元格不是全視圖,所以這比你想象的要複雜得多。如果你真的必須用細胞做到這一點,這裏解釋:NSButtonCell inside custom NSCell

但是...如果你可以限制自己到10.7+,他們已經添加了基於視圖的表格。這些要簡單得多,因爲您可以在NSTableViewCellView的內部放置一個完整的NSButton。這在Table View Programming Guide中有解釋。強烈建議,如果你可以限制自己10.7 +。

+0

事實證明,這比我想象的要複雜得多。不幸的是,我也必須在10.6上支持一些人,所以我想我必須採用第一種解決方案。謝謝! – houbysoft 2012-03-03 04:11:28