简体   繁体   中英

SwiftUI ForEach.onDelete properly animates but then resets

Trying to figure out why my ForEach loop isn't updating. I have the model marked as ObservedObject and have done everything I could to make sure the updates were happening. I even saw that the model was being updated while printing.

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!

I figured it out. I needed to make the array in my model object with the @Published property wrapper. Doing that fixed everything!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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