问题
It easy to create a simple list like this:
List { Text("Item 1") Text("Item 2") Text("Item3") }
But how I can add a selection like traditional NSTable?
回答1:
struct names {
var id = 0
var name = ""
}
var demoData = ["Phil Swanson", "Karen Gibbons", "Grant Kilman", "Wanda Green"]
struct SelectionDemo : View {
@State var selectKeeper = Set<String>()
var body: some View {
HStack {
List(demoData, id: \.self, selection: $selectKeeper){ name in
Text(name)
}.frame(width: 500, height: 460)
}
}
}
#if DEBUG
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
SelectionDemo()
}
}
#endif
来源:https://stackoverflow.com/questions/57549007/swiftui-mac-os-how-to-add-selection-to-the-list-view