【问题标题】:SwiftUI clear button for all Toggles in a List列表中所有切换的 SwiftUI 清除按钮
【发布时间】:2022-06-14 23:48:27
【问题描述】:

我是 SwiftUI 新手,这是我想要实现的目标:

我有一个带有部分的Form。每个部分都有一个列表,每一行都有一个Toggle 我可以选择/取消选择。在列表下方,我有一个清除按钮。单击一下,我想将所有Toggles 重置为取消选择状态。

模型(每个 ListSection 代表 Form 中的 Section):

struct RowItem: Identifiable {
    let id = UUID()
    var isSelected: Bool
    var rowName: String
    var amount: Int
}

struct ListSection: Identifiable, Equatable {
    static func == (lhs: ListSection, rhs: ListSection) -> Bool {
        lhs.id == rhs.id
    }
    
    let id = UUID()
    let title: String
    let items: [RowItem]
    let visible: Int
}

class HomeModel: ObservableObject {
    private let items1 = [RowItem(isSelected: false, rowName: "Energy", amount: 24),
                          RowItem(isSelected: true, rowName: "Automotive", amount: 70),
                          RowItem(isSelected: true, rowName: "Materials", amount: 56),
                          RowItem(isSelected: false, rowName: "Industrials", amount: 109),
                          RowItem(isSelected: true, rowName: "Consumer1", amount: 209),
                          RowItem(isSelected: true, rowName: "Consumer2", amount: 12),]

    private let items2 = [RowItem(isSelected: true, rowName: "Europe", amount: 302),
                          RowItem(isSelected: true, rowName: "America", amount: 589),
                          RowItem(isSelected: false, rowName: "Asia", amount: 67),
                          RowItem(isSelected: false, rowName: "Africa", amount: 207),
                          RowItem(isSelected: true, rowName: "Oceania", amount: 9)]

    @Published var sections: [ListSection]

    init() {
        self.sections = [ListSection(title: "Sectors", items: items1, visible: 4),
                         ListSection(title: "Regions", items: items2, visible: 5)]
    }
}

struct ExpandableList: View {
    private var title: String
    private var visibleElements: Int
    private var showAllButtonVisible: Bool
    
    @State private var items: [RowItem]
    @State private var isExpanded = false
    
    init(title: String, items: [RowItem], visible: Int) {
        self.title = title.lowercased()
        self.items = items
        self.visibleElements = visible
        self.showAllButtonVisible = visible < items.count
    }
    
    var body: some View {
        List {
            ForEach(isExpanded ? 0..<items.count : 0..<Array(items[0..<visibleElements]).count, id: \.self) { index in
                HStack {
                    Text(items[index].rowName).fontWeight(.light)
                    Spacer()
                    Text("\(items[index].amount)").foregroundColor(.gray).fontWeight(.light)
                    Toggle(isOn: Binding(
                        get: { return items[index].isSelected },
                        set: { items[index].isSelected = $0 }
                    )){}.toggleStyle(.checklist)
                }.listRowSeparator(.hidden)
            }
            
            if showAllButtonVisible {
                HStack {
                    Button {
                        withAnimation {
                            isExpanded.toggle()
                        }
                    } label: {
                        if isExpanded {
                            Label("Show less \(title)", systemImage: "chevron.up").foregroundColor(Color(red: 55/255, green: 126/255, blue: 83/255, opacity: 1)).labelStyle(ImageToRightSideLabelStyle())
                        } else {
                            Label("Show all \(title)", systemImage: "chevron.down").foregroundColor(Color(red: 55/255, green: 126/255, blue: 83/255, opacity: 1)).labelStyle(ImageToRightSideLabelStyle())
                        }
                    }
                }
            }
        }
    }
}

struct MyForm: View {
    @ObservedObject var model = HomeModel()
    
    var body: some View {
        NavigationView {
            VStack {
                Form {
                    ForEach(model.sections, id: \.id) { section in
                        Section {
                            ExpandableList(title: section.title, items: section.items, visible: section.visible)
                        } header: {
                            Text(section.title).font(.title3).foregroundColor(.black).textCase(nil)
                        } footer: {
                            if section != model.sections.last {
                                VStack {
                                    Divider().padding(.top, 15)
                                }
                            }

                        }
                        
                    }
                }.navigationTitle(Text("Filter stocks"))
                
                HStack {
                    Button {
                        for section in model.sections {
                            for var item in section.items {
                                item.isSelected = false
                                print("Tag1 false")
                            }
                        }
                    } label: {
                        Text("Clear filters").foregroundColor(.black)
                    }.padding(.leading, 30)
                    Spacer()
                    Button(action: {
                        print("sign up bin tapped")
                    }) {
                        Text("Show 1506 assets")
                            .font(.system(size: 18))
                            .padding()
                            .foregroundColor(.black)
                    }
                    .background(Color.green) // If you have this
                    .cornerRadius(10)
                    .padding(.trailing, 30)
                }
            }

        }
    }
}

我假设我需要在某个地方使用Binding,但不确定在哪里。

【问题讨论】:

  • 需要提供view的所有数据相关代码,因为不清楚具体在哪里。
  • 我添加了所有代码。

标签: ios swift swiftui


猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-11
  • 2012-10-16
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
相关资源
最近更新 更多