Select Multiple Items in SwiftUI List

前端 未结 6 936
遥遥无期
遥遥无期 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:13

    I created a custom ToggleStyle as follows:

    import SwiftUI
    
    
    enum Fruit: String, CaseIterable, Hashable {
        case apple = "Apple"
        case orange = "Orange"
        case banana = "Banana"
    }
    
    struct ContentView: View {
    
        @State var fruits = [Bool](repeating: false, count: Fruit.allCases.count)
    
        var body: some View {
            Form{
                ForEach(0.. some View {
            HStack {
                Button(action: { withAnimation { configuration.$isOn.wrappedValue.toggle() }}){
                    HStack{
                        configuration.label.foregroundColor(.primary)
                        Spacer()
                        if configuration.isOn {
                            Image(systemName: "checkmark").foregroundColor(.primary)
                        }
                    }
                }
            }
        }
    }
    
    

提交回复
热议问题