简体   繁体   English

SwiftUI ForEach.onDelete 正确设置动画但随后重置

[英]SwiftUI ForEach.onDelete properly animates but then resets

Trying to figure out why my ForEach loop isn't updating.试图弄清楚为什么我的 ForEach 循环没有更新。 I have the model marked as ObservedObject and have done everything I could to make sure the updates were happening.我将 model 标记为 ObservedObject 并尽我所能确保更新正在发生。 I even saw that the model was being updated while printing.我什至看到 model 在打印时正在更新。

class Model {
    var array: [Int]
}
…
struct ModelView: View {
    @ObservedObject var model: Model
var body: some View {
    List {
         ForEach(model.array.indices, id:\.self) { index in
…
        }.onDelete(perform: delete)
    }
}

The row is animating and acting like it is deleting and does delete in the model, however the deleted row animates back in and the original data set is shown!该行正在动画并且表现得好像它正在删除并且确实在 model 中删除,但是被删除的行重新动画并显示原始数据集!

I figured it out.我想到了。 I needed to make the array in my model object with the @Published property wrapper.我需要使用@Published 属性包装器在我的 model object 中创建数组。 Doing that fixed everything!这样做解决了一切!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM