我正在尝试基于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() }
}
没有完全提供解决方案的两个相关问题: