更新 TextField 中的值并相应地更新其数组索引

编程入门 行业动态 更新时间:2024-10-08 18:35:42
本文介绍了更新 TextField 中的值并相应地更新其数组索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我无法更新通过 for 每个循环显示的数组中的值.这些值显示在文本字段中.

I am having trouble updating values in an array that are displayed via a for each loop. These values are displayed in a text field.

有问题的代码

struct EditItemView: View { @State var recipeStep: [String] @State var stepInfo: String = "" @State var textFieldCount: Int = 1 @State var stepNumber: [Int] @State var recordsCount = 2 var body: some View { //a bunch of code between here and the list, does not apply VStack { List { ForEach(0..<recipeStep.count, id: \.self) { index in HStack { Text(String(stepNumber[index]) + ".").bold() EditorViewEdit(container: self.$recipeStep, index: index, text: recipeStep[index]) } }.onDelete { (indexSet) in stepNumber.remove(atOffsets: indexSet) recipeStep.remove(atOffsets: indexSet) }

ForEach 循环中的以下代码段是我用来使列表中的每个文本字段可编辑的代码(因为我不知道有任何其他方法可以使文本字段在列表中工作):

The following piece of code in the ForEach loop is what I use to make each of the text fields editable in the list (as I do not know of any other way to make text fields work in a list):

EditorViewEdit(container: self.$recipeStep, index: index, text: recipeStep[index])

上面代码中引用的结构体如下:

The struct referenced in the above code can be seen below:

struct EditorViewEdit : View { var container: Binding<[String]> var index: Int @State var text: String var body: some View { TextField("", text: self.$text, onCommit: { self.container.wrappedValue[self.index] = self.text }) }

}

问题

如何更改 foreach 循环中的文本字段并使其在 @State var recipeStep: [String] 中的值相应更新?例如,我在 for each 循环中编辑第一个 TextField - 如何使用新值更新数组中的索引 0?

How can I make a change to a textfield in a foreach loop and have its value in @State var recipeStep: [String] be updated accordingly? For example, I edit the first TextField in the for each loop - how can I update index 0 in the array with its new value?

推荐答案

有很多方法可以在 foreach 循环中更改文本字段并在recipeStep中有它的价值.尝试这样的事情:

there are many ways to make a change to a textfield in a foreach loop and have its value in recipeStep. Try something like this:

import SwiftUI @main struct TestApp: App { var body: some Scene { WindowGroup { ContentView() } } } struct ContentView: View { @State var recipeStep = ["recipe step 1","recipe step 2","recipe step 3"] var body: some View { VStack { EditItemView(recipeStep: $recipeStep) Text(recipeStep.description) } } } struct EditItemView: View { @Binding var recipeStep: [String] var body: some View { VStack { List { ForEach(recipeStep.indices, id: \.self) { index in HStack { Text(String(index) + ".").bold().foregroundColor(.red) TextField("Recipe step", text: $recipeStep[index]) } }.onDelete { indexSet in recipeStep.remove(atOffsets: indexSet) } } } } }

更多推荐

更新 TextField 中的值并相应地更新其数组索引

本文发布于:2023-11-11 08:31:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1577892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   索引   TextField

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!