2015-02-11 144 views
6

在我的應用程序中,我有一個基於NSTableView的視圖,只有一列。行的高亮顏色設置爲常規(藍色)。我需要將該顏色更改爲我的自定義顏色。在界面生成器中,我試着改變它,但唯一的選擇是「無,常規和源列表」。Cocoa osx NSTableview更改行高亮顏色

我嘗試沒有成功這篇文章的解決方案: https://stackoverflow.com/a/9594543/3065901

我讀,我要利用這個委託方法,但我不知道如何使用這個。

- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row 

我試圖在該方法中繪製行,但我得到無效的上下文警告,行仍然保持相同higlight。 請發表一個簡單的如何使用這種代表方法的例子:

請幫助。提前致謝。

+0

你使用基於視圖基礎的tableView或細胞。 – Srinidhi 2015-02-11 21:42:34

+0

我使用View Based – user3065901 2015-02-12 08:59:25

回答

8

drawRow樣品從這個link

MyNSTableRowView.h

#import <Cocoa/Cocoa.h> 
@interface MyNSTableRowView : NSTableRowView 
@end 

MyNSTableRowView.m

#import "MyNSTableRowView.h" 

@implementation MyNSTableRowView 
- (id)init 
{ 
    if (!(self = [super init])) return nil; 
    return self; 
} 

- (void)drawSelectionInRect:(NSRect)dirtyRect { 
    if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone) { 
    NSRect selectionRect = NSInsetRect(self.bounds, 2.5, 2.5); 
    [[NSColor colorWithCalibratedWhite:.65 alpha:1.0] setStroke]; 
    [[NSColor colorWithCalibratedWhite:.82 alpha:1.0] setFill]; 
    NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect 
                   xRadius:6 yRadius:6]; 
    [selectionPath fill]; 
    [selectionPath stroke]; 
    } 
} 
@end 

AppDelegate.m

- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row 
{   
    MyNSTableRowView *rowView = [[MyNSTableRowView alloc]init]; 
    return rowView; 
} 
2

如果您使用的是基於細胞的tableview,設置NSTableView的selectionHighLightStyle爲無 NSTableView的,並覆蓋以下

- (void)drawRow:(NSInteger)row clipRect:(NSRect)clipRect { 
     NSColor* bgColor = Nil; 
    // Set the color only when its first responder and key window 
    if (self == [[self window] firstResponder] && [[self window] isMainWindow] && [[self window] isKeyWindow]) 
    { 
     bgColor = [NSColor brownColor]; 
    } 
    else 
    { 
     bgColor = [NSColor windowBackgroundColor];; 
    } 

    NSIndexSet* selectedRowIndexes = [self selectedRowIndexes]; 
    if ([selectedRowIndexes containsIndex:row]) 
    { 
     [bgColor setFill]; 
     NSRectFill([self rectOfRow:row]); 
    } 
    [super drawRow:row clipRect:clipRect]; 
} 
7

這裏是user3065901的斯威夫特卡倫特3回答:

class MyNSTableRowView: NSTableRowView { 

    override func drawSelection(in dirtyRect: NSRect) { 
     if self.selectionHighlightStyle != .none { 
      let selectionRect = NSInsetRect(self.bounds, 2.5, 2.5) 
      NSColor(calibratedWhite: 0.65, alpha: 1).setStroke() 
      NSColor(calibratedWhite: 0.82, alpha: 1).setFill() 
      let selectionPath = NSBezierPath.init(roundedRect: selectionRect, xRadius: 6, yRadius: 6) 
      selectionPath.fill() 
      selectionPath.stroke() 
     } 
    } 
} 

NSTableViewDelegate:

func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? { 
    return MyNSTableRowView() 
} 
+0

你太棒了!感謝這個解決方案。 – ixany 2016-11-24 18:23:24

+0

這很好,但我遇到的麻煩是,即使表格處於「模糊」狀態,行突出顯示仍保持該顏色。例如,如果選擇一行,然後單擊一個「NSTextField」,表格行仍然高亮顯示,但文本再次變黑。這裏是一個視頻示例:https://d.pr/v/0KmToP ...任何想法如何改變該州的行背景顏色? – 2017-08-14 22:30:39

0

添加下面一行在您的實現代碼如下方法

的ObjectiveC

[yourtableview setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone]; 

斯威夫特

yourtableview.selectionHighlightStyle = .none