Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Mac OS how to add selection to the List view

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?

like image 989
Nayef Avatar asked Aug 18 '19 23:08

Nayef


1 Answers

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
like image 185
Nayef Avatar answered Nov 15 '22 08:11

Nayef