我正在尝试创建基于List和ScrollView的表视图,其中包含一个共享的基本视图。我目前的尝试使用了基于构建器的方法,但需要使用AnyView类型擦除的闭包。
我的目标是避免使用AnyView类型擦除,以提高性能、可伸缩性和设计。
到目前为止,我使用泛型参数重新设计的尝试都失败了。
下面的简化(和工作)代码示例只显示一个String列,但是捕捉到了基本的挑战。(实际上,这些是多列表,具有不同的数据类型、专门的格式化程序、修饰符等)
import SwiftUI
struct ContentView: View {
let data = ["a", "b", "c", "d"]
var body: some View {
HStack {
ListTable(title: "My List", data: data)
ScrollTable(title: "My Scroll", data: data)
}
}
}
struct ListTable<T: Hashable>: View {
var title: String, data: [T]
var body: some View {
BaseTable() { header, row in
VStack {
header(title)
List(data, id: \.self) { row($0) }
}
}
}
}
struct ScrollTable<T: Hashable>: View {
var title: String, data: [T]
var body: some View {
BaseTable() { header, row in
VStack {
header(title)
ScrollView { ForEach(data, id: \.self) { row($0) } }
}
}
}
}
struct BaseTable<Content: View, T: Hashable>: View {
typealias HBuilder = (String) -> AnyView
typealias RBuilder = (T) -> AnyView
var builder: (@escaping HBuilder, @escaping RBuilder) -> Content
var body: some View {
// NOTE this is where I'd like to avoid type-erasure to AnyView
builder({ AnyView( Text("Header \($0)") )},
{ AnyView( Text("Row \(String(describing: $0))") ) })
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View { ContentView() }
}两个相关的问题没有提供很好的解决办法:
发布于 2022-02-09 17:00:18
我对我自己的问题有一个很好的答案。
为行创建一个“包装器”视图,它将服务于行生成器闭包中使用的显式类型:
// new 'wrapper' view, which can contain Text, LazyVGrid, etc.
struct RowView<T: Hashable>: View {
var value: T
var body: some View {
Text("Row \(String(describing: value))")
}
}
// update to original BaseTable
struct BaseTable<Content: View, T: Hashable>: View {
typealias HBuilder = (String) -> Text
typealias RBuilder = (T) -> RowView<T>
var builder: (@escaping HBuilder, @escaping RBuilder) -> Content
var body: some View {
builder({ Text("Header \($0)") },
{ RowView<T>(value: $0) })
}
}感谢评论员们!
https://stackoverflow.com/questions/71044305
复制相似问题