thomedy
thomedy

Reputation: 95

I can't delete my last table view cell in Swift

I am writing a shopping cart and everything seems to be working well but I can't delete the last row.

I can delete but not the last row.

override func viewDidLoad() {
    super.viewDidLoad()

    createLineItems()
}

override func numberOfSections(in tableView: UITableView) -> Int {
    if lineItems.count == 0 {
        return 0
    } else {
        return 1
    }
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return lineItems.count
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if(editingStyle == .delete) {
        lineItems.remove(at: indexPath.row)

        tableView.beginUpdates()

        let indPath = IndexPath(item: indexPath.row, section: 0 )

        tableView.deleteRows(at: [indPath], with: .fade)
        tableView.endUpdates()

    }
}

func createLineItems() {
    lineItems = [LineItem]()

    lineItems.append(LineItem(frame:
        CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height),
                              index: 1)
        )


    lineItems.append(LineItem(frame:
        CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height),
                              index: 2)
    )

    lineItems.append(LineItem(frame:
        CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height),
                              index: 3)
    )

}

The error I get is printing out:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (0) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'

I have read:

https://www.hackingwithswift.com/example-code/uikit/how-to-remove-cells-from-a-uitableview

as well as other pages that I can't re-find.

I am aware I have to remove elements from my array first and then delete the row.

I am aware that I am pretty sure I have to surround my row deletion with beginUpdates and endUpdates.

But I get that error every time. I have tried 100 different variations.

The error says the row count has to be 1 less than the original number.

I have no idea how it is not.

Upvotes: 0

Views: 712

Answers (1)

rmaddy
rmaddy

Reputation: 318824

The problems is here:

override func numberOfSections(in tableView: UITableView) -> Int {
    if lineItems.count == 0 {
        return 0
    } else {
        return 1
    }
}

It should be:

override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

If you delete the last row your code is claiming there are now no sections. But you are not deleting any sections, only rows. This is confusing the table view and resulting in the error.

Upvotes: 3

Related Questions