How can I connect NSTableCellView.textField to a SwiftUI view?

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+.

You can’t directly assign NSHostingView to the textField property of NSTableCellView because it expects an NSTextField, and a hosting view is a NSView object. Instead, you could use your hosting view as the cell and include a text field and image either as a Label or LabeledContent, or you could provide the AppKit equivalent of those in your SwiftUI view.

You can't directly assign NSHostingView to the textField property of NSTableCellView because it expects an NSTextField, and a hosting view is a NSView object.

I'm aware of that. The pseudo code above was trying to ask how I could connect a property or view from rootView to either the cell's textField or the imageView.

Instead, you could use your hosting view as the cell and include a text field and image either as a Label or LabeledContent

If I create a SwiftUI View who's body consists of a Label and assign that SwiftUI view to the rootView of an NSHostingView which I return from the NSOutlineView's delegate (in place of returning an NSTableCellView), the NSOutlineView does not recognize the Label as the textField in the same way it does in AppKit.

Specifically, changes to the "Sidebar Icon Size" in the Settings app are not reflected in this SwiftUI view. The size of the label's text and image do not automatically update, as they do in AppKit.

Creating a SwiftUI cell view and then populating it with an NSTextField and an NSImageView kind of defeats the purpose of using SwiftUI here. I might as well just use an NSTableCellView, no?

How can I connect NSTableCellView.textField to a SwiftUI view?
 
 
Q