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?
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))
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
I'm trying to add help files to my Mac OS application. I've been reading the documentation below, but I'm not sure how to apply to my project as there isn't any Resources folder:
https://developer.apple.com/library/archive/documentation/Carbon/Conceptual/ProvidingUserAssitAppleHelp/authoring_help/authoring_help_book.html#//apple_ref/doc/uid/TP30000903-CH206-CIHEHEAC
Is there an updated version of Apple Documentation on how to create help files?