I have a NSTableView under a NSViewRepresentable and I'm trying to add a new row when the + button is clicked.
With the code below, the ForEach statement is updated when the data is added to the state variable, but the table view doesn't show the new element.
Why is that?
With the code below, the ForEach statement is updated when the data is added to the state variable, but the table view doesn't show the new element.
Why is that?
Code Block import PlaygroundSupport import SwiftUI struct TV: NSViewRepresentable { @Binding var students: Array<String> func makeNSView(context: Context) -> NSScrollView { let sv = NSScrollView() let tv = NSTableView() sv.documentView = tv tv.gridStyleMask = .solidHorizontalGridLineMask tv.usesAlternatingRowBackgroundColors = true// tv.allowsMultipleSelection = true let col = NSTableColumn(identifier: NSUserInterfaceItemIdentifier(rawValue: "Name")) col.title = "Name" _ = tv.addTableColumn(col) _ = tv.delegate = context.coordinator tv.dataSource = context.coordinator return sv } func updateNSView(_ nsView: NSScrollView, context: Context) { (nsView.documentView as! NSTableView).reloadData() } func makeCoordinator() -> Coordinator { Coordinator(self, students: self._students) } class Coordinator: NSObject, NSTableViewDelegate, NSTableViewDataSource { var parent: TV @Binding var students: Array<String> init(_ parent: TV, students: Binding<Array<String>>) { self.parent = parent self._students = students } func numberOfRows(in tableView: NSTableView) -> Int { return students.count } func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> Any? { print(students[row]) return students[row] } func tableView(_ tableView: NSTableView, shouldEdit tableColumn: NSTableColumn?, row: Int) -> Bool { return true } func tableView(_ tableView: NSTableView, setObjectValue object: Any?, for tableColumn: NSTableColumn?, row: Int) { self.students[row] = object as! String } } } struct view: View { @State var students = ["John", "Mary", "Bob"] @State var minusDisabled: Bool = true var body: some View { Group { VStack(alignment: .leading) { Text("Test") TV(students: self.$students).frame(width: 400, height: 300) HStack { Button(action: { self.students.append("New Row") }) { Text("+") }.buttonStyle(BorderedButtonStyle()) Button(action: { }) { Text("-") }.buttonStyle(BorderedButtonStyle()) .disabled(self.minusDisabled) } }.fixedSize() ForEach(self.students, id: \.self) { student in Text(student) } } } } PlaygroundPage.current.setLiveView(view().frame(width: 500, height: 800))