SwiftUi 我无法删除项目 [英] SwiftUi I can not delete item

查看:17
本文介绍了SwiftUi 我无法删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么会发生错误.addItem() 有效,但在执行 removeItem() 时发生错误.

I can't understand why the error occurs. The addItem() works, but an error occurs when removeItem() is executed.

MyViewModel

class MyViewModel: ObservableObject {
    @Published var items:[MyModel] = []
}


我的模型

struct MyModel : Identifiable {
    var id: Int
    var title:String
    var value:String
}


内容视图

struct ContentView: View {

@StateObject var model = MyViewModel()

var body: some View {
    VStack {
        Button(action: {
            addItem()
        }) {
            Text("add")
        }
        ForEach(0..<model.items.count, id: \.self) { index in
            HStack {
            TextField("value", text: Binding(get: { self.model.items[index].value },
                                        set: { self.model.items[index].value = $0 }))
                Button(action: {
                    removeItem()
                }) {
                    Text("delete")
                }
            }
        }
    }
}
func addItem() {
    self.model.items.append(MyModel(id: +1, title: "", value: ""))
}
func removeItem() {
    if let index = model.items.first?.id {
        model.items.remove(at: index)
    }
}
}

我认为问题出在 func removeItem()

推荐答案

问题出在ForEach

如苹果 doc

ForEach :实例只读取提供数据的初始值,不需要跨更新识别视图.

ForEach : The instance only reads the initial value of the provided data and doesn’t need to identify views across updates.

因此,当您从数组中删除一个对象时,您已经更改了数组,但 SwiftUI 没有看到该更改,而是使用了原始数组.

So when you remove an object from the array, you have changed the array but SwiftUI doesn't see that change and it uses the original array.

因此,您只需添加类似这样的条件 index < 即可修复它.self.model.items.count ?self.model.items[index].value : ";

So, you just fix it by just adding conditions like this index < self.model.items.count ? self.model.items[index].value : ""

另外,我建议在 ForEach

还有一点你不需要每次删除时都找到索引.只需将索引传递给函数即可.

One more point you no need to find index every time on delete. Just pass the index to function.

这是完整的代码.

struct ContentView: View {
    @StateObject var model = MyViewModel()
    var body: some View {
        VStack {
            Button(action: {
                addItem()
            }) {
                Text("add")
            }
            ForEach(model.items.indices, id:\.self) { index in //< -- Here
                HStack {
                    TextField("value", text: Binding(get: {  index < self.model.items.count ? self.model.items[index].value : "" }, //<-- Here
                                                     set: { self.model.items[index].value = $0 }))
                    Button(action: {
                        removeItem(at: index) //< -- Here
                    }) {
                        Text("delete")
                    }
                }
            }
        }
    }
    func addItem() {
        self.model.items.append(MyModel(title: "", value: ""))
    }
    
    func removeItem(at index: Int) {
        model.items.remove(at: index) //< -- Here
    }
}

这篇关于SwiftUi 我无法删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆