[英]Swiftui foreach inside parameter
how to foreach inside the view parameter如何在视图参数中foreach
this is my code这是我的代码
CarouselView(itemHeight: 124, views: [
SliderItemView(),
SliderItemView(),
SliderItemView(),
])
i want to iterate and show the SliderItemView with looping我想迭代并循环显示 SliderItemView
ForEach
cannot create an array. ForEach
无法创建数组。 If you want to create an array of those views one way to do it would be [0..<3].map { _ in SliderItemView() }
如果你想创建这些视图的数组,一种方法是
[0..<3].map { _ in SliderItemView() }
Here 2 way for solving your issue:这里有 2 种方法可以解决您的问题:
struct ContentView: View {
var body: some View {
CustomViewV1(content: {
ForEach(0...2, id:\.self) { _ in Text("Hello, World!").foregroundColor(Color.blue) }
})
CustomViewV2(repeating: 3, content: { Text("Hello, World!").foregroundColor(Color.red) })
}
}
struct CustomViewV1<Content: View>: View {
@ViewBuilder let content: () -> Content
var body: some View {
return content()
}
}
struct CustomViewV2<Content: View>: View {
let repeating: Int
@ViewBuilder let content: () -> Content
var body: some View {
if (repeating >= 1) {
ForEach(1...repeating, id:\.self) { _ in
content()
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.