Home IOS Development ios – Flawed merchandise will get deleted from LazyVGrid

ios – Flawed merchandise will get deleted from LazyVGrid

ios – Flawed merchandise will get deleted from LazyVGrid


My objective is to create an inventory like this in SwiftUI

LazyVGrid SwiftUI Swipe to delete

Each time the person faucets add, a brand new row is created after which on swiping to delete, the respective row must be deleted.

With a purpose to do this, I begin with a really primary mannequin

struct SnagItem: Codable {
    personal(set) var reference: Int = 0
    personal(set) var particulars: String = ""
    personal(set) var isCompleted: Bool = false
    enum CodingKeys: String, CodingKey, CaseIterable {
        case reference = "Reference"
        case particulars = "Particulars"
        case isCompleted = "IsCompleted"

extension SnagItem: Hashable { }

I take advantage of this mannequin inside a view mannequin which I want to bind to my SwiftUI view, that is the view mannequin:

class SnagItemViewModel: ObservableObject {
    personal let snagItem: SnagItem
    @Revealed var particulars: String = ""
    @Revealed var isCompleted: Bool = false
    init(withSnagItem snagItem: SnagItem) {
        self.snagItem = snagItem
        particulars = snagItem.particulars
        isCompleted = snagItem.isCompleted

I’ve one other view mannequin that retains monitor of all of the snag objects (rows) utilizing an array. This additionally handles the including and deletion of things.

class SnagItemsViewModel: ObservableObject {
    @Revealed var snagItems: [SnagItemViewModel] = [SnagItemViewModel(withSnagItem: SnagItem())]
    func getSnagItem(at index: Int) -> SnagItemViewModel {
    func addSnagItem() {
        snagItems.append(SnagItemViewModel(withSnagItem: SnagItem()))
    func deleteSnagItemRecord(at indexSet: IndexSet) {
        guard let index = indexSet.first else { return }
        snagItems.take away(at: index)
@Revealed var snagItems: [SnagItemViewModel] = [SnagItemViewModel(withSnagItem: SnagItem())]

I’ve created a SwiftUI view that acts as a container:

struct SnagItemsView: View {
    @StateObject var snagItemsViewModel: SnagItemsViewModel
    var physique: some View {
        ForEach(0 ..< snagItemsViewModel.snagItems.depend, id: .self) { snagItemIndex in
            let snagItemViewModel = snagItemsViewModel.getSnagItem(at: snagItemIndex)
            SnagItemRowView(reference: snagItemIndex + 1,
                            snagItemViewModel: snagItemViewModel)
        .onDelete(carry out: delete(at:))
    func delete(at indexSet: IndexSet) {
        withAnimation {
            snagItemsViewModel.deleteSnagItemRecord(at: indexSet)

Every row then has it is personal view:

struct SnagItemRowView: View {
    @State var reference: Int
    @StateObject var snagItemViewModel: SnagItemViewModel
    let gridItemColumnConfig = [
    var physique: some View {
        LazyVGrid(columns: gridItemColumnConfig, spacing: 8) {
            ForEach(0 ..< SnagItem.CodingKeys.allCases.depend, id: .self) { columnIndex in
                let snagItemCategory = SnagItem.CodingKeys.allCases[columnIndex]
                if snagItemCategory == .reference {
                    Textual content("(reference)")
                } else if snagItemCategory == .particulars {
                    TextField(snagItemViewModel.localizedText(for: .enterValue),
                              textual content: $snagItemViewModel.particulars,
                              axis: .vertical)
                } else {
                    Toggle("", isOn: $snagItemViewModel.isCompleted)

The whole lot appears to work superb for essentially the most half.

It begins off like this:

LazyVGrid SwiftUI

When the person faucets Add, one other row is added and doing this a number of instances provides extra rows:

LazyVGrid rows and colums matrix SwiftUI

The problems start, when I attempt to delete:

LazyVGrid swipe to delete List SwiftUI

As you may see I am making an attempt to delete row 3, after deletion that is the result’s that row 4 will get deleted as a substitute, atleast from a UI perspective:

LazyVGrid column matrix SwiftUI with switch

I printed out the values from my get operate and it appears that evidently the view mannequin truly appears to have the right objects, the SwiftUI View does not appear to render the right knowledge nonetheless for some cause:

XCode LazyVGrid SwiftUI

I added a breakpoint in my delete operate, to see if the correct index was being handed and it looks like every thing appears to be like good right here:

XCode breakpoint swiftUI lazyVGrid delete index

Lastly, when I attempt to add one other row after the deletion course of, the outdated knowledge appears to be added again once more:

LazyVGrid SwiftUI rows and columns

And once more, after I verify the view mannequin, the info appears to be proper, however the SwiftUI view reveals one thing else.

What am I doing incorrect and the way might I repair this ?



Please enter your comment!
Please enter your name here