macOS 上的 SwiftUI - 对 Table 列中的每一行使用 Picker
SwiftUI on macOS - using Picker for each row in a Table Column
我在 macOS(SwiftUI 3.0 中的新功能)上的 Table 中显示了一些项目。在每列中显示 Text
视图时,我可以让 table 正常工作,但我真的想在其中一列中显示 Picker
视图。但我不确定如何 'bind' 选择器选择数组中用于驱动项目列表的项目。
这是我正在尝试的代码:
struct TestSwiftUIView: View {
@State var mappingFields: [ImportMappingField]
var body: some View {
VStack {
Table(mappingFields) {
TableColumn("Imported Field") { item in
Text("\(item.columnName)")
}.width(160)
TableColumn("Mapped Field") { item in
Text("\(item.fieldName.typeNameAndDescription.description)")
}.width(150)
TableColumn("Type") { item in
Picker("", selection: $item.selectedCustomFieldType){ // error here
ForEach(ImportMappingType.allCases, id:\.self ) { i in
Text(i)
}
}
}.width(100)
}
}
}
}
在 selection: $item.selectedCustomFieldType
中,我收到一条错误消息“无法在范围内找到‘$item’”。
所以我想将每个选择器绑定到每个 'item' 中的一个元素,但它不允许我这样做。
这个问题有好的解决办法吗?
尝试使用此方法为您提供所需的 item
绑定:
Table($mappingFields) { // <-- here
TableColumn("Type") { $item in // <-- here
Picker("", selection: $item.selectedCustomFieldType){
...
}
另一个 TableColumn
也类似。您可能必须使 ImportMappingField
符合 Identifiable
我在 macOS(SwiftUI 3.0 中的新功能)上的 Table 中显示了一些项目。在每列中显示 Text
视图时,我可以让 table 正常工作,但我真的想在其中一列中显示 Picker
视图。但我不确定如何 'bind' 选择器选择数组中用于驱动项目列表的项目。
这是我正在尝试的代码:
struct TestSwiftUIView: View {
@State var mappingFields: [ImportMappingField]
var body: some View {
VStack {
Table(mappingFields) {
TableColumn("Imported Field") { item in
Text("\(item.columnName)")
}.width(160)
TableColumn("Mapped Field") { item in
Text("\(item.fieldName.typeNameAndDescription.description)")
}.width(150)
TableColumn("Type") { item in
Picker("", selection: $item.selectedCustomFieldType){ // error here
ForEach(ImportMappingType.allCases, id:\.self ) { i in
Text(i)
}
}
}.width(100)
}
}
}
}
在 selection: $item.selectedCustomFieldType
中,我收到一条错误消息“无法在范围内找到‘$item’”。
所以我想将每个选择器绑定到每个 'item' 中的一个元素,但它不允许我这样做。
这个问题有好的解决办法吗?
尝试使用此方法为您提供所需的 item
绑定:
Table($mappingFields) { // <-- here
TableColumn("Type") { $item in // <-- here
Picker("", selection: $item.selectedCustomFieldType){
...
}
另一个 TableColumn
也类似。您可能必须使 ImportMappingField
符合 Identifiable