xcode 如何在swiftUI中使用ForEach内的条件语句

bweufnob  于 2023-01-21  发布在  Swift
关注(0)|答案(1)|浏览(139)

如何在swiftUI中的ForEach内使用条件语句?是否可以?

    • 预期内容**
  • 我的期望是根据ID(foodItem)显示正确的食物项,因为我能够获得模型类中的所有食物项 *

使用案例

  • 我在GridView中使用ForEach来显示不同的食物项目,我将所有食物项目都放在模型中,并传递变量"foodItem",以便网格视图了解需要显示的食物类型 *

当前版本
因为我有foodItem的id,所以我在forEach中使用了一个条件语句来显示正确的foodItem,但是Xcode给了我一个错误,2个错误。
我所做的代码

LazyVGrid(columns: columns, spacing: 20) {
                                ForEach(
                                    if(foodType == 1){
                                        model.foodItems
                                    }else if ( foodType == 2){
                                        model.drinkItems
                                    }
                                    else{
                                        model.dessers
                                    }
 id: \.self) { item in
                            VStack{
                                GridImageView(item.image)
                                Text(item.food)
                                
                                
                            }
                            
                        }
                    }
                    .padding(.horizontal)
    • 我的模型类**
import Foundation

class Model: ObservableObject {
    let foodItems = [
        FoodItems(food: "Rice",
                  image: "ricee"),
        FoodItems(food: "Fish Curry",
                  image: "fishcurry"),
        FoodItems(food: "Fish Curry 2",
                  image: "Fish Curry 2"),
        FoodItems(food: "Egg Curry",
                  image: "eggcurry")
    ]

    let drinkItems = [
        DrinkItems(food: "Cola",
                   image: "cola"),
        DrinkItems(food: "Pepsi",
                   image: "pepsi"),
        DrinkItems(food: "Limca",
                   image: "limca"),
    ]

    let dessers = [
        Desserts(food: "strawberry",
                 image: "strawberry"),
        Desserts(food: "chocolate",
                 image: "chocolate"),
    ]
}
  • 先谢了 *
5ktev3wc

5ktev3wc1#

除了尝试使用在ForEach中不起作用的条件语句之外,您还可以使用其他方法。
对数据使用常规模型(Food),并使用filter选择要显示的type,如以下示例代码所示:

struct ContentView: View {
    @StateObject var model = Model()
    @State var foodType = 1
    
    var body: some View {
        // for testing
        Button("Change type") {
            foodType = foodType + 1
            if foodType > 3 { foodType = 1 }
        }.buttonStyle(.bordered)
        
        ForEach(model.foodItems.filter{$0.type == foodType}) { item in
            VStack{
                Text(item.food)
                // image....
            }
        }
    }
}

struct Food: Identifiable, Hashable {
    let id = UUID()
    var type: Int
    var food: String
    var image: String
}

class Model: ObservableObject {
    let foodItems = [
        Food(type: 1, food: "Rice", image: "ricee"),
        Food(type: 1, food: "Fish Curry", image: "fishcurry"),
        Food(type: 1, food: "Fish Curry 2", image: "Fish Curry 2"),
        Food(type: 1, food: "Egg Curry", image: "eggcurry"),
        Food(type: 2, food: "Cola", image: "cola"),
        Food(type: 2, food: "Pepsi", image: "pepsi"),
        Food(type: 2, food: "Limca", image: "limca"),
        Food(type: 3, food: "strawberry", image: "strawberry"),
        Food(type: 3, food: "chocolate", image: "chocolate")
    ]
}

更好的是,对type使用枚举。

相关问题