我想要一个包含行的列表,每行里面有 2 个文本字段。这些行应该保存在一个数组中,以便我可以在其他视图中使用数据来实现进一步的功能。如果文本字段中的文本发生更改,则文本应保存在数组的右侧条目内。您还可以通过按钮将新行添加到列表中,这也会更改行的数组。
目标是拥有一个键值对列表,每个键值对都可编辑,并且这些条目与当前文本一起保存。
有人可以帮助我和/或给我解决这个问题的提示吗?
到目前为止我已经尝试过这样的事情:
// the array of list entries
@State var list: [KeyValue] = [KeyValue()]
Run Code Online (Sandbox Code Playgroud)
// the List inside of a VStack
List(list) { entry in
KeyValueRow(list.$key, list.$value)
}
Run Code Online (Sandbox Code Playgroud)
// the single item
struct KeyValue: Identifiable {
var id = UUID()
@State var key = ""
@State var value = ""
}
Run Code Online (Sandbox Code Playgroud)
// one row in the list with view elements
struct KeyValueRow: View {
var keyBinding: Binding<String>
var valueBinding: Binding<String>
init(_ keyBinding: Binding<String>, _ valueBinding: Binding<String>){ …Run Code Online (Sandbox Code Playgroud)