繁体   English   中英

SwiftUI 获取 ForEach 循环中的下一项

[英]SwiftUI get next item in ForEach loop

我正在尝试实现一个 VStack 网格,当 ForEach 循环中的下一个项目满足条件时,该网格需要一个 HStack。 我有下面的代码显示数组中的项目,但我不知道如何获取下一个要检查的项目。

这是我到目前为止所拥有的。

VStack {
            ForEach(item.cards, id: \.self) { item in
                switch item.card.size {
                case .Large:
                    LargeCard(card: item.card, viewModel: CardViewModel())
                case .Medium:
                    MediumCard(card: item.card, viewModel: CardViewModel())
                case .Small:
                    HStack(spacing: 16) {
                        SmallCard(card: item.card, viewModel: CardViewModel())
                        // This is where I think I need to check and I tried somthing like below, but the compiler crashes
                        if $0 + 1 < item.cards.count {
                            if item.card.size == .Small {
                                SmallCard(card: item.card, viewModel: CardViewModel())
                            }
                        }
                    }
                case .none:
                    Text("No more.")
                }
            }
        }

这是项目结构:

struct Item: Decodable, Hashable {
    let card: Card
}

这就是我想要得到的。

在此处输入图像描述

您可以尝试将id添加到您的 struct Item ,例如:

struct Item: Identifiable, Decodable, Hashable {
    let id = UUID()
    let card: Card
}

然后使用:

VStack {
    ForEach(item.cards, id: \.self) { theItem in
        switch theItem.card.size {
        case .Large:
            LargeCard(card: theItem.card, viewModel: CardViewModel())
        case .Medium:
            MediumCard(card: theItem.card, viewModel: CardViewModel())
        case .Small:
            HStack(spacing: 16) {
                SmallCard(card: theItem.card, viewModel: CardViewModel())
                // here use the id to find the next item
                if let ndx = item.cards.firstIndex(where: {$0.id == theItem.id}) {
                    if ndx + 1 < item.cards.count {
                        let nextItem = item.cards[ndx + 1]
                        if nextItem.card.size == .Small {
                            SmallCard(card: nextItem.card, viewModel: CardViewModel())
                        }
                    }
                }
            }
        case .none:
            Text("No more.")
        }
    }
}

您还可以使用评论中提到的枚举,例如:

VStack {
    ForEach(Array(item.cards.enumerated()), id: \.offset) { index, theItem in
        switch theItem.card.size {
        case .Large:
            LargeCard(card: theItem.card, viewModel: CardViewModel())
        case .Medium:
            MediumCard(card: theItem.card, viewModel: CardViewModel())
        case .Small:
            HStack(spacing: 16) {
                SmallCard(card: theItem.card, viewModel: CardViewModel())
                // here use the index to find the next item
                if index + 1 < item.cards.count {
                    let nextItem = item.cards[index + 1]
                    if nextItem.card.size == .Small {
                        SmallCard(card: nextItem.card, viewModel: CardViewModel())
                    }
                }
            }
        case .none:
            Text("No more.")
        }
    }
}

请注意,您似乎应该使用.environmentObject(viewModel)将单个CardViewModel()传递给视图,而不是每次都创建一个新的CardViewModel()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM