Select Multiple Items in SwiftUI List

前端 未结 6 921
遥遥无期
遥遥无期 2021-02-04 11:37

In UIKit you can select multiple rows of a UITableView by using allowsMultipleSelection - can this be done with the List in SwiftUI?

6条回答
  •  后悔当初
    2021-02-04 12:33

    First add this to your view

    @State var selectedItems = Set()
    

    The type of the Set depends on the type you use to id: the items in the ForEach

    Next declare the List

    List(selection: $selectedItems) {
        ForEach(items, id: \.id) { item in
            Text("\(item.name)")
        }
    }
    

    Now whatever you select gets added to the selectedItems Set remember to clear it out after you use it.

提交回复
热议问题