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?
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 more simplified example of the app:
import SwiftUI
struct NSTableViewWrapper: NSViewRepresentable {
class Coordinator: NSObject, NSTableViewDataSource, NSTableViewDelegate {
var parent: NSTableViewWrapper
init(parent: NSTableViewWrapper) {
self.parent = parent
}
func numberOfRows(in tableView: NSTableView) -> Int { 1 }
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
return NSTextField(labelWithString: "Item")
}
func tableView(_ tableView: NSTableView, rowActionsForRow row: Int, edge: NSTableView.RowActionEdge) -> [NSTableViewRowAction] {
return [NSTableViewRowAction(style: .destructive, title: "Delete") { _, _ in }]
}
}
func makeCoordinator() -> Coordinator {
return Coordinator(parent: self)
}
func makeNSView(context: Context) -> NSScrollView {
let scrollView = NSScrollView()
let tableView = NSTableView()
let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("Column"))
tableView.addTableColumn(column)
tableView.delegate = context.coordinator
tableView.dataSource = context.coordinator
scrollView.documentView = tableView
return scrollView
}
func updateNSView(_ nsView: NSScrollView, context: Context) {
(nsView.documentView as? NSTableView)?.reloadData()
}
}
struct ContentView: View {
var body: some View {
NSTableViewWrapper()
}
}
And here is the demo:
There are two things which can be done:
1:
Add another tableView method into the Coordinator which looks like this:
func tableView(_ tableView: NSTableView, rowViewForRow row: Int) -> NSTableRowView? {
return MyCustomRowView()
}
And MyCustomRowView
would just have a isEmphasized
property override:
class MyCustomRowView: NSTableRowView {
override var isEmphasized: Bool {
set {}
get { false }
}
}
It can not give us any selection color we want, but it does change the color. To gray:
2:
Replace property override with a method override in the MyCustomRowView
from the code above:
class MyCustomRowView: NSTableRowView {
override func drawSelection(in dirtyRect: NSRect) {
NSColor.brown.setFill()
self.bounds.fill()
}
}
It changes the background, but not the background we had initially. It completely ignores the interaction with the swipe action: