Since there's been no reply, here's most of the code for the NSViewControllerRepresentable class:
Swift
struct LayoutsTableView: NSViewControllerRepresentable {
typealias NSViewControllerType = LayoutsTableController
@Binding var layoutsList: [LayoutData]
@Binding var selectedIndex: Int
func makeNSViewController(context: Context) - LayoutsTableController {
let theTable = LayoutsTableController(nibName: "LayoutsTable", bundle: Bundle.main)
theTable.layoutList = layoutsList
theTable.tableDelegate = context.coordinator // So that the table can set its delegate after it appears
context.coordinator.tableController = theTable
return theTable
}
func makeCoordinator() - Coordinator {
return Coordinator(self)
}
func updateNSViewController(_ nsViewController: LayoutsTableController, context: Context) {
nsViewController.layoutsTable.reloadData()
}
final class Coordinator: NSObject, NSTableViewDelegate {
var parentView: LayoutsTableView
var tableController: LayoutsTableController?
init(_ parent: LayoutsTableView) {
parentView = parent
}
// MARK: Table Delegate methods
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) - NSView? {
// returns the appropriate view
}
func tableViewSelectionDidChange(_ notification: Notification) {
let selectedRow = tableController?.layoutsTable.selectedRow ?? -1
parentView.selectedIndex = selectedRow
}
}
}
Any clues, anyone?