[英]NSTableRowView/NSTableCellView how to set custom color to selected row?
我正在嘗試在選擇表行時實現自定義行顏色。
-(void)tableViewSelectionDidChange:(NSNotification *)notification{
NSInteger selectedRow = [_mainTable selectedRow];
NSTableCellView *cell = [_mainTable rowViewAtRow:selectedRow makeIfNecessary:NO];
cell.layer.backgroundColor = [NSColor redColor].CGColor;
NSLog(@"selected");
}
但這不起作用。 我發現Apple文檔很混亂(也許我錯了)。 我對Mac編程沒有經驗。
有人建議任何解決方案? 基本上我需要選擇顏色是透明的。
解
這應該通過NSTableRowView
然后使用NSTableView
委托方法返回子類來完成
-(NSTableRowView*)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
在修改行視圖時,子類化NSTableRowView
提供了更大的靈活性。 在上面的NSTableView
委托方法中返回您的子NSTableView
也會在從一行到另一行的單擊時自動刪除背景選擇顏色(這是在提供的另一個答案中的一個未解決的問題)。
腳步
首先,子類NSTableRowView
並覆蓋drawSelectionInRect
以在選擇時更改其背景顏色:
@implementation MyTableRowView
- (void)drawSelectionInRect:(NSRect)dirtyRect
{
[super drawSelectionInRect:dirtyRect];
[[NSColor yellowColor] setFill];
NSRectFill(dirtyRect);
}
接下來,使用rowViewForRow
NSTableView
委托方法返回子類行視圖:
- (NSTableRowView*)tableView:(NSTableView *)tableView rowViewForRow:(NSInteger)row
{
static NSString* const kRowIdentifier = @"MyTableRow";
MyTableRowView* myRowView = [tableView makeViewWithIdentifier:kRowIdentifier owner:self];
if (!myRowView) {
myRowView = [[MyTableRowView alloc] initWithFrame:NSZeroRect];
myRowView.identifier = kRowIdentifier;
}
return rowView;
}
使用此方法,您還可以輕松覆蓋其他元素,如分隔符顏色。 為此,請覆蓋NSTableRowView
子類中的drawSeparatorInRect
方法,如下所示:
- (void)drawSeparatorInRect:(NSRect)dirtyRect
{
// Change the separator color if the row is selected
if (self.isSelected) [[NSColor orangeColor] setFill];
else [[NSColor grayColor] setFill];
// Fill the seperator
dirtyRect.origin.y = dirtyRect.size.height - 1.0;
dirtyRect.size.height = 1.0;
NSRectFill(dirtyRect);
}
資源
覆蓋NSTableRowView
顯示設置https://developer.apple.com/reference/appkit/nstablerowview
NSTableview rowViewForRow
委托方法https://developer.apple.com/reference/appkit/nstableviewdelegate/1532417-tableview
首先設置tableview選擇高亮樣式
NSTableViewSelectionHighlightStyleNone
然后在你的tablView委托工具中
tableView:shouldSelectRow:
並在其中編寫此代碼:
NSTableViewRow *row= [_mainTable rowViewAtRow:selectedRow makeIfNecessary:NO];
row.backgroundColor = [your color];
return YES;
這是將自定義顏色設置為選定的行以及突出顯示的文本顏色。 輸出應該看起來像這樣,
在上面的截圖中,我們正在做
將背景選定顏色設置為白色
添加角半徑
將文本顏色更改為藍色
添加藍色筆觸顏色
你可以做更多的定制,但這個答案涵蓋了上述幾點。
1.從子類化NSTableRowView開始
class CategoryTableRowView: NSTableRowView {
override func drawSelection(in dirtyRect: NSRect) {
if selectionHighlightStyle != .none {
let selectionRect = bounds.insetBy(dx: 2.5, dy: 2.5)
NSColor(calibratedRed: 61.0/255.0, green: 159.0/255.0, blue: 219.0/255.0, alpha: 1.0).setStroke()
NSColor(calibratedWhite: 1.0, alpha: 1.0).setFill()
let selectionPath = NSBezierPath(roundedRect: selectionRect, xRadius: 25, yRadius: 25)
selectionPath.fill()
selectionPath.stroke()
}
}
}
2.在NSTableViewDelegate方法中返回自定義CategoryTableRowView()
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
return CategoryTableRowView()
}
3.確保在ViewController類中定期使用selectionHighlightStyle
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.selectionHighlightStyle = .regular
}
4.要設置textColor,請創建NSTableCellView的子類
class CategoryCellView: NSTableCellView {
@IBOutlet weak var categoryTextField: NSTextField!
override var backgroundStyle: NSView.BackgroundStyle {
willSet{
if newValue == .dark {
categoryTextField.textColor = NSColor(calibratedRed: 61.0/255.0, green: 159.0/255.0, blue: 219.0/255.0, alpha: 1.0)
} else {
categoryTextField.textColor = NSColor.black
}
}
}
}
覆蓋backgroundStyle
屬性並為文本設置所需的顏色。
注意:在我的情況下,我有一個自定義單元格,它有一個categoryTextField
outlet.So來設置我使用的文本顏色: categoryTextField.textColor = NSColor.black
我希望這有幫助。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.