When using NSTableView or NSOutlineView, if you use an NSTableCellView and wire up the .imageView and .textField properties then you get some "free" behaviour with respect to styling and sizing of those fields. (ex: They reflect the user's preferred "Sidebar Icon Size" as selected in Settings. )
If I'm using a SwiftUI View inside an NSTableCellView, is there any way to connect a Text or Image to those properties?
Consider the following pseudo code:
struct MyCellView: View {
let text: String
let url: URL?
var body: some View {
HStack {
Image(...) // How to indicate this is .imageView?
Text(...) // How to indicate this is .textField?
}
}
}
final class MyTableCellView: NSTableCellView {
private var hostingView: NSHostingView<MyCellView>!
init() {
self.hostingView = NSHostingView(rootView: MyCellView(text: "", url: nil))
self.addSubview(self.hostingView)
}
func configureWith(text: String, url: URL) {
let rootView = MyCellView(text: text, url: url)
hostingView.rootView = rootView
// How can I make this connection?
self.textField = rootView.???
self.imageView = rootView.???
}
}
I'm ideally looking for a solution that works on macOS 15+.