更新列表中的一行 (SwiftUI)
Posted
技术标签:
【中文标题】更新列表中的一行 (SwiftUI)【英文标题】:Update a row in a list (SwiftUI) 【发布时间】:2020-04-07 14:23:52 【问题描述】:我是编程的早起者,所以我知道从专家的角度来看这个问题可能很荒谬,但我几天后就陷入了这种情况。 在使用另一个按钮使用 TextField 存储项目后,我想通过使用按钮“编辑”(铅笔)来更新一行。
代码如下:
class Food: Hashable, Codable, Equatable
var id : UUID = UUID()
var name : String
init(name: String)
self.name = name
static func == (lhs: Food, rhs: Food) -> Bool
return lhs.name == rhs.name
func hash(into hasher: inout Hasher)
hasher.combine(name)
class Manager: ObservableObject
let objectWillChange = PassthroughSubject<Void, Never>()
@Published var shoppingChart: [Food] = []
init()
let milk = Food(name: "Milk")
let coffee = Food(name: "Coffee")
shoppingChart.append(milk)
shoppingChart.append(coffee)
func newFood(name: String)
let food = Food(name: name)
shoppingChart.insert(food, at: 0)
struct ContentView: View
@ObservedObject var dm : Manager
@State var isAddFoodOpened = false
var body: some View
VStack
List
ForEach(self.dm.shoppingChart, id:\.self) food in
HStack
Text(food.name)
Image(systemName: "pencil")
self.buttonAdd
var buttonAdd: some View
Button(action:
self.isAddFoodOpened.toggle()
)
Text("Add")
.sheet(isPresented: $isAddFoodOpened)
Add(dm: self.dm, fieldtext: "", isAddFoodOpened: self.$isAddFoodOpened)
struct Add: View
@ObservedObject var dm : Manager
@State var fieldtext : String = ""
@Binding var isAddFoodOpened : Bool
var body: some View
VStack
TextField("Write a food", text: $fieldtext)
buttonSave
var buttonSave : some View
Button(action:
self.dm.newFood(name: self.fieldtext)
self.isAddFoodOpened = false
)
Text("Save")
【问题讨论】:
只是一个不相关的注释,如果与 Food 类的其他对象相比,使 food 对象独一无二的是名称,那么将其设为id
属性并避免创建像:var id: String name
或在你的初始化中设置id
。所以,当你需要食物的 id 时,只需返回 name。
【参考方案1】:
@ObservedObject var dm : Manager
对象永远不会被初始化。
尝试像这样在 ContentView 中初始化 dm:
@ObservedObject var dm = Manager()
【讨论】:
【参考方案2】:好的,如果我理解正确,您想使用“编辑”按钮更新/编辑一行。 这样就可以了:
struct ContentView: View
@ObservedObject var dm : Manager
@State var isAddFoodOpened = false
@State var isEditOpened = false
@State var fieldtext : String = ""
var body: some View
VStack
List
ForEach(0..<self.dm.shoppingChart.count, id:\.self) i in
HStack
Text(self.dm.shoppingChart[i].name)
Button(action: self.isEditOpened.toggle() )
Image(systemName: "pencil")
.sheet(isPresented: self.$isEditOpened)
TextField(self.dm.shoppingChart[i].name, text: self.$fieldtext, onEditingChanged: _ in
self.dm.shoppingChart[i].name = self.fieldtext
)
self.buttonAdd
var buttonAdd: some View
Button(action:
self.isAddFoodOpened.toggle()
)
Text("Add")
.sheet(isPresented: $isAddFoodOpened)
Add(dm: self.dm, fieldtext: "", isAddFoodOpened: self.$isAddFoodOpened)
【讨论】:
如何保存 TextField 的更改并返回 Content View? TextField的变化保存在:self.dm.shoppingChart[i].name = self.fieldtext以上是关于更新列表中的一行 (SwiftUI)的主要内容,如果未能解决你的问题,请参考以下文章