SwiftUI中的列表的多个过滤器/切换

时间:2019-08-08 06:23:45

标签: swift swiftui

我在SWiftUI中创建了一个动态列表,并且插入了两个切换开关来过滤列表。但是,如何使两个切换都影响同一个列表并不是很简单,因为{}列表周围的切换括号是

我已经尝试过将第二个切换包装在第一个周围,但这是行不通的。

        HStack {
        Toggle(isOn: $hideReds) {
            Text("Hide reds")
            .foregroundColor(.red)
        }
        .padding(.horizontal, 30)

        Toggle(isOn: $hideWhitess) {
            Text("Hide whites")
                .foregroundColor(.blue)

        }                .padding(.horizontal, 22)

        }
        List {
            Section {
                ForEach(AGrapes) { grape in

                    if !self.hideReds || grape.isRed {

                GrapeCell(grape: grape)
                }
                }
                .onDelete(perform: delete)
                .onMove(perform: move)
            }

        }

我认为我需要添加:

   if !self.hideWhites || grape.isWhite {}

但是在哪里?

2 个答案:

答案 0 :(得分:0)

您可以像这样在传递列表之前进行过滤。

var filteredGrapes: [Grape] {
    return AGrapes.filter({ (grape) -> Bool in
        return !((self.hideReds && grape.isRed) || (self.hideWhitess && grape.isWhite))
    })
}

var body: some View {
    VStack{
        HStack {
            Toggle(isOn: $hideReds) {
                Text("Hide reds")
                    .foregroundColor(.red)
            }
            .padding(.horizontal, CGFloat(30))

            Toggle(isOn: $hideWhitess) {
                Text("Hide whites")
                    .foregroundColor(.blue)

            }                .padding(.horizontal, 22)

        }
        List {
            Section {
                ForEach(filteredGrapes) { grape in
                    GrapeCell(grape: grape)
                }
                .onDelete(perform: delete)
                .onMove(perform: move)
            }

        }
    }
}

答案 1 :(得分:0)

您应该使用过滤器:

import SwiftUI

struct Grape: Identifiable {
    let id = UUID()
    let name: String
    let isRed: Bool
}

struct ContentView: View {
    @State private var AGrapes: [Grape] = [Grape(name: "Merlot", isRed: true), Grape(name: "Cabernet Sauvignon", isRed: true), Grape(name: "Pinot Noir", isRed: true), Grape(name: "Albariño", isRed: false), Grape(name: "Sauvignon Blanc", isRed: false)]

    @State private var hideReds = false
    @State private var hideWhitess = false

    var body: some View {
        VStack {
            HStack {
                Toggle(isOn: $hideReds) {
                    Text("Hide reds").foregroundColor(.red)
                }
                .padding(.horizontal, 30)

                Toggle(isOn: $hideWhitess) {
                    Text("Hide whites").foregroundColor(.blue)
                }.padding(.horizontal, 22)

            }

            List {
                Section {
                    ForEach(AGrapes.filter({ return (!self.hideWhitess && !$0.isRed) || (!self.hideReds && $0.isRed) })) { grape in                        GrapeCell(grape: grape)
                    }
                }

            }
        }
    }
}

struct GrapeCell: View {
    let grape: Grape

    var body: some View {
        HStack {
            Circle().fill(grape.isRed ? Color.red : Color.green).frame(width: 30)
            Text(grape.name)
            Spacer()
        }

    }
}