2015-02-11 33 views
6

Trong ứng dụng của tôi, tôi có chế độ xem dựa trên NSTableView với một cột. Màu tô sáng của các hàng được đặt thành màu xanh (thường). Tôi cần thay đổi màu đó thành màu tùy chỉnh của tôi. Trong trình xây dựng giao diện, tôi đã thử thay đổi nó nhưng các tùy chọn duy nhất là "Không, danh sách thường xuyên và nguồn".Cocoa osx NSTableview thay đổi màu tô sáng hàng

tôi đã cố gắng giải pháp này bài không thành công: https://stackoverflow.com/a/9594543/3065901

tôi đọc mà tôi phải sử dụng phương pháp đại biểu này nhưng tôi không biết làm thế nào để sử dụng này.

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

Tôi đã thử vẽ hàng trong phương thức đó nhưng tôi nhận cảnh báo ngữ cảnh không hợp lệ và hàng vẫn giữ nguyên với cùng một higlight. Vui lòng đăng một ví dụ đơn giản về cách sử dụng phương thức ủy nhiệm này:

Cần trợ giúp. Cảm ơn trước.

+0

Bạn đang sử dụng xem dựa tableView hoặc tế bào dựa. – Srinidhi

+0

Im sử dụng Xem dựa – user3065901

Trả lời

8

Từ link này.

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

Nếu bạn đang sử dụng di động dựa tableview, thiết lập NSTableView selectionHighLightStyle tới Không NSTableView, và ghi đè lên mẫu drawRow dưới

- (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

Dưới đây là câu trả lời user3065901 trong Swift 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

Bạn thật tuyệt vời! Cảm ơn vì giải pháp này. – ixany

+0

Điều này hoạt động khá tốt, nhưng sự cố tôi gặp phải là việc làm nổi bật hàng giữ nguyên màu đó ngay cả khi bảng ở trạng thái "mờ". Ví dụ: nếu bạn chọn một hàng, sau đó nhấp vào một 'NSTextField', hàng của bảng vẫn được tô sáng nhưng văn bản sẽ tối lại. Đây là một đoạn video ví dụ: https://d.pr/v/0KmToP ... bất kỳ ý tưởng làm thế nào để thay đổi màu nền của hàng đó? –

0

Thêm bên dưới dòng trong phương pháp tableview bạn

ObjectiveC

[yourtableview setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone]; 

Swift

yourtableview.selectionHighlightStyle = .none 
Các vấn đề liên quan