【问题标题】:Cocoa osx NSTableview change row highlight colorCocoa osx NSTableview 改变行高亮颜色
【发布时间】:2015-04-12 05:42:34
【问题描述】:

在我的应用程序中,我有一个基于视图的 NSTableView,其中包含一列。行的突出显示颜色设置为常规(蓝色)。我需要将该颜色更改为我的自定义颜色。在界面生成器中,我尝试更改它,但唯一的选项是“无,常规和源列表”。

我尝试了这个帖子解决方案但没有成功: https://stackoverflow.com/a/9594543/3065901

我读到我必须使用这个委托方法,但我不知道如何使用它。

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

我尝试在该方法中绘制该行,但我收到无效的上下文警告,并且该行仍然保持相同的亮点。 请发布一个如何使用此委托方法的简单示例:

需要帮助。提前致谢。

【问题讨论】:

  • 你使用的是基于视图的tableView还是基于单元格的。

标签: macos cocoa colors nstableview highlight


【解决方案1】:

来自这个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】:

    这是 user3065901 在 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()
    }
    

    【讨论】:

    • 你太棒了!感谢您提供此解决方案。
    • 这很好用,但我遇到的麻烦是,即使表格处于“模糊”状态,行突出显示仍保持该颜色。例如,如果您选择一行,然后单击NSTextField,表格行仍然突出显示,但文本再次变暗。这是一个示例视频:d.pr/v/0KmToP ...知道如何更改该状态的行背景颜色吗?
    【解决方案3】:

    如果您使用基于单元格的表格视图,请将 NSTableView selectionHighLightStyle 设置为 None NSTableView,并覆盖下面的drawRow示例

    - (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]; 
    }
    

    【讨论】:

      【解决方案4】:

      在您的 tableview 方法中添加以下行

      目标C

      [yourtableview setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleNone];
      

      斯威夫特

      yourtableview.selectionHighlightStyle = .none
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-07
        • 1970-01-01
        相关资源
        最近更新 更多