可可osx NSTableview更改行突出显示颜色 [英] Cocoa osx NSTableview change row highlight color
问题描述
在我的应用程序中,我有一个带有一列的基于NSTableView的视图.行的突出显示颜色设置为常规(蓝色).我需要将该颜色更改为我的自定义颜色.在界面构建器中,我尝试更改它,但是唯一的选项是无,常规和源列表".
In my application I have a view based NSTableView with one column. The highlight color of the rows is set to regular (blue). I need to change that color to my custom color. In the interface builder I tried changing it but the only options are "None, regular and source list".
我尝试了以下帖子解决方案,但没有成功: https://stackoverflow.com/a/9594543/3065901
I tried this post solution with no success: https://stackoverflow.com/a/9594543/3065901
我读到我必须使用此委托方法,但我不知道如何使用它.
I read that I have to use this delegate method but I dont know how to use this.
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
我尝试用该方法绘制该行,但收到无效的上下文警告,并且该行仍保持相同的背景光. 请发布一个有关如何使用此委托方法的简单示例:
I tried drawing the row in that method but i get invalid context warnings and the row still keeps with the same higlight. Please post a simple example of how to use this delegate method:
请帮助.预先感谢.
推荐答案
通过此链接.
MyNSTableRowView.h
MyNSTableRowView.h
#import <Cocoa/Cocoa.h>
@interface MyNSTableRowView : NSTableRowView
@end
MyNSTableRowView.m
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
AppDelegate.m
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
MyNSTableRowView *rowView = [[MyNSTableRowView alloc]init];
return rowView;
}
这篇关于可可osx NSTableview更改行突出显示颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!