SwiftUI Mac OS how to add selection to the List view

巧了我就是萌 提交于 2020-12-10 08:57:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!