NSTableView默認(rèn) NSTableViewSelectionHighlightStyle
NSTableview有三種選中模式
typedef NS_ENUM(NSInteger, NSTableViewSelectionHighlightStyle) {
NSTableViewSelectionHighlightStyleNone NS_ENUM_AVAILABLE_MAC(10_6) = -1,
NSTableViewSelectionHighlightStyleRegular = 0,
NSTableViewSelectionHighlightStyleSourceList = 1,
默認(rèn)模式為NSTableViewSelectionHighlightStyleRegular在此種模式下,行選中顏色有l(wèi)ight blue ([NSColor alternateSelectedControlColor]) 和 light gray color ([NSColor secondarySelectedControlColor])兩種。
在開(kāi)發(fā)過(guò)程中昙沦,根據(jù)需求會(huì)有改變選中狀態(tài)下行背景色的情況。下面為將介紹兩種改變背景色的方法楞件。
-
繼承NSTableRowView
繼承NSTableRowView ,重寫(xiě)- (void)drawSelectionInRect:(NSRect)dirtyRect 方法裳瘪。
- (void)drawSelectionInRect:(NSRect)dirtyRect {
if (self.selectionHighlightStyle != NSTableViewSelectionHighlightStyleNone) {
NSRect selectionRect = NSInsetRect(self.bounds, 0, 0);
[[NSColor yellowColor] setFill];
NSBezierPath *selectionPath = [NSBezierPath bezierPathWithRoundedRect:selectionRect xRadius:0 yRadius:0];
[selectionPath fill];
}
}
實(shí)現(xiàn)協(xié)議NSTableViewDelegate的- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row方法
- (NSTableRowView *)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
YLTableRowView *rowView = [[YLTableRowView alloc] init];
return rowView;
}
- 繼承NSTableCellView土浸, 重寫(xiě)- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle方法cellView的backgroudStyle由NSTableRowView自動(dòng)設(shè)置,可根據(jù)backgroudStyle的類(lèi)型對(duì)cellView的背景色進(jìn)行控制彭羹。
The backgroundStyle property is automatically set by the enclosing NSTableRowView to let this view know what its background looks like. For instance, when the -backgroundStyle is NSBackgroundStyleDark, the view should use a light text color.
- (void)setBackgroundStyle:(NSBackgroundStyle)backgroundStyle
{
[super setBackgroundStyle:backgroundStyle];
if(backgroundStyle == NSBackgroundStyleDark)
{
self.backgroundColor = [NSColor yellowColor];
}
else
{
self.backgroundColor = [NSColor whiteColor];
}
}