简体   繁体   English

如何在 SwiftUI 中使用 Foreach 视图?

[英]How to use Foreach on view in SwiftUI?

I'm developing my first iOS App, using Xcode 11.3 and Swift5.我正在开发我的第一个 iOS 应用程序,使用 Xcode 11.3 和 Swift5。

But I was stuck in using ForEach on view.但我一直坚持在视图中使用 ForEach。

I want to put certain struct in ZStack using Foreach but still get errors.我想使用 Foreach 将某些struct放在 ZStack 中,但仍然会出错。

Error : Unable to infer complex closure return type; add explicit type to disambiguate

I don't know how to solve this.我不知道如何解决这个问题。 Can you help me?你能帮助我吗?

/* Struct I want to put in */
struct Verses: Identifiable{
    var id: Int
    var verse : Int
}
/* I want to load all values from struct using foreach in view. */
        ZStack {
            ForEach(controller.verses) { w in              <- Here is where I get error.

                Rectangle()
                    .foregroundColor(Color.white)
                .cornerRadius(28)
                    .opacity(0.4)
                    .offset(x:0, y:68)
                    .frame(width:290, height:280)

                CardView(date: w.date)
                .gesture(DragGesture()
                .onChanged({ (value) in
......


ForEach row must be represented by single View, so you need something like the following (still I'm not sure in type of container, but just for example) ForEach行必须由单个视图表示,因此您需要类似以下内容(我仍然不确定容器的类型,但仅作为示例)

ForEach(controller.verses) { w in
  ZStack {
    Rectangle()
        .foregroundColor(Color.white)
    .cornerRadius(28)
        .opacity(0.4)
        .offset(x:0, y:68)
        .frame(width:290, height:280)

    CardView(date: w.date)
    .gesture(DragGesture()
    .onChanged({ (value) in

    ...
  }
}

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

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