2013-10-24 31 views
7

我正在開發一個MAC應用程序幷包含tableView。 想要將所選行的顏色更改爲黃色。NSTableview更改高亮顏色

+0

這可能有幫助 - http://stackoverflow.com/questions/7038709/change-highlighting-color-in- nstableview-in-cocoa –

回答

13

設置這對您的表視圖:

[yourtableview setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone]; 

並實行NSTableView的以下委託方法爲:

- (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex 
{ 
    if ([[aTableView selectedRowIndexes] containsIndex:rowIndex]) 
    { 
     [aCell setBackgroundColor: [NSColor yellowColor]]; 
    } 
    else 
    { 
     [aCell setBackgroundColor: [NSColor whiteColor]]; 
    } 
    [aCell setDrawsBackground:YES]; 
} 
+0

謝謝......我會試試這個.. :) – Raghav

+5

假設你正在談論一個基於單元格的表格,這跟以前的答案不一樣,除非你的單元格碰巧沒有任何填充(甚至可能不是)。要更改行突出顯示,請覆蓋表的highlightSelectionInClipRect:方法並自己繪製背景顏色,如下所示:http://stackoverflow.com/questions/7038709/change-highlighting-color-in-nstableview-in-cocoa – dgatwood

0

如果你想heighlight列的只有個別細胞然後實現這樣的下面: -

- (void)tableView:(NSTableView *)tableView 
    willDisplayCell:(id)cell 
    forTableColumn:(NSTableColumn *)tableColumn 
       row:(NSInteger)row 
{ 
    if ([[tableColumn identifier] isEqualToString:@"yourColumm"]) 
    { 
     [cell setBackgroundColor:[NSColor yelloColor]]; 
    } 
} 
+0

據我所知,所選行中單元格的背景完全被行高亮所覆蓋,因此設置單元格的背景顏色並不會改變單元格的外觀。 – dgatwood