Consider the code from my previous question: https://developer.apple.com/forums/thread/776592
How can I change the background color of a focused item?
Here is a solution i found:
- Remove the highlight style of your tableView like this:
tableView.selectionHighlightStyle = .none
- Set different background in
tableViewSelectionDidChangemethod like this:
func tableViewSelectionDidChange(_ notification: Notification) {
guard let tableView = notification.object as? NSTableView else { return }
guard tableView.selectedRow >= 0 else {
for i in 0..<tableView.numberOfRows {
guard let rowView = tableView.rowView(atRow: i, makeIfNecessary: false) else {
return
}
rowView.backgroundColor = .clear
}
return
}
guard let rowView = tableView.rowView(atRow: tableView.selectedRow, makeIfNecessary: false) else {
return
}
rowView.backgroundColor = .red.withAlphaComponent(0.1)
}
The full code can be found here.