-1

我正在尝试构建一个自定义滚动视图,该视图可以接收我创建的不同单元格并在滚动视图中显示这些单元格。我无法弄清楚如何将通用 SwiftUI 视图传递到我的自定义滚动视图结构的构造函数中,是否可以这样做?有些像这样:

struct CustomScroll<Content: View>: View {
      var genericCell: View

      var body: some View {
        VStack(spacing: 10) {
            ForEach(0...7, id: \.self){ index in
                VStack(spacing: 10){
                    cell
                }
            }
        }
    }

}
4

2 回答 2

0

我想你正在寻找这样的东西:

struct CustomScroll<Content: View>: View {
    
    @ViewBuilder let content: () -> Content
    
    var body: some View {
        ScrollView {
            VStack(spacing: 10) {
                content()
            }
        }
    }
}

并像这样使用它:

struct ContentView: View {
    var body: some View {
        CustomScroll {
            Text("Test 1")
            Text("Test 2")
            Text("Test 3")
            Text("Test 4")
            Text("Test 5")
        }
    }
}
于 2022-02-25T20:51:53.487 回答
0

您可以为此创建一个自定义单元格!

struct CustomCellView: View {
var object: Object = Object(nombre: "")
init(m: Object) {
    object = m
}
var body: some View {
    VStack {
        HStack {
            Text(object.descripcion).font(.system(size: 11)).frame(width: 105, alignment: .trailing)
        }
        Divider()
    }
}

}

然后在滚动视图中调用它

 ForEach(objects, id: \.objectId) { object in
                    CustomCellView(m: object)
                }.padding(EdgeInsets(top: 3, leading: 0, bottom: 0,   trailing: 0))
于 2022-02-25T17:06:41.533 回答