But when I display my list of items in the sidebar, I can't select any items. I can click on them of course, but the row selection highlighting doesn't work at all.
I think maybe this might have something to do with the fact that my SwiftUI view is embedded within an NSHostingView.
I've setup my NSHostingView like this:
Code Block import Cocoa import SwiftUI @objc class FormsListHostingController: NSViewController { public var viewModel : FormsListUIView.ViewModel? var formsListView : FormsListUIView? @objc public var databaseDocument : TFDatabaseDocument? override func viewDidLoad() { super.viewDidLoad() var hostingView : NSHostingView<FormsListUIView>? if let context = self.databaseDocument?.dataAccessManager?.persistentContainer.viewContext { let viewModel = FormsListUIView.ViewModel(managedObjectContext: context) let contentView : FormsListUIView = FormsListUIView(viewModel: viewModel) hostingView = NSHostingView(rootView: contentView) self.view = hostingView! } } }
and my SwiftUI view is setup like this:
Code Block struct FormsListUIView: View { @Environment(\.managedObjectContext) var context @ObservedObject var viewModel: ViewModel @State private var selection: CDForm? = nil @State private var isSelected : Bool = false var body: some View { List(selection: self.$selection) { ForEach(viewModel.categories, id: \.uuid) { (category : CDCategory) in Section(header: Text(category.name ?? "").font(.headline)) { ForEach(Array(category.forms as! Set<CDForm>), id: \.uuid) { (form : CDForm) in FormRow(form: form).onTapGesture { self.selection = form isSelected.toggle() } } } } } .listStyle(SidebarListStyle()) .frame(maxWidth: .infinity, maxHeight: .infinity) } }
Any ideas why the row selection doesn't show? I'm suspecting it's a problem with showing a SwiftUI view within an NSHostingView.