繁体   English   中英

SwiftUI中的ForEach

[英]ForEach in SwiftUI

当使用ScrollView和ForEach时,如下代码所示:

ScrollView(.horizontal,showsIndicators: false) {
                HStack {
                    ForEach(self.QUOTES, id: \.self) { qt in
                        Text(qt)
                    }
                }
            }

没关系,没问题。 但是当我想在ForEach块中使用任何代码(例如添加图像)时,例如以下代码:

ScrollView(.horizontal,showsIndicators: false) {
                HStack {
                    ForEach(self.QUOTES, id: \.self) { qt in
                        Text(qt)
                        Image(systemName: "a.square.fill")
                               .resizable()
                               .frame(width: 40, height: 40)
                    }
                }
            }

编写时出现以下错误:无法推断复杂的闭包返回类型; 添加显式类型以消除歧义

ForEach结构需要包含一个视图,因此您需要根据希望的布局呈现方式对文本和图像进行分组或将它们嵌入堆栈中。

ScrollView(.horizontal,showsIndicators: false) {
    HStack {
        ForEach(self.QUOTES, id: \.self) { qt in
            Group {
                Text(qt)
                Image(systemName: "a.square.fill")
                       .resizable()
                       .frame(width: 40, height: 40)
            }
        }
    }
}

暂无
暂无

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

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