I am trying to remove a item from my TableView but when I swipe to left and click in Delete, I got this error:
This is the code that I have, when I comment the self.tableView.deleteRowsAtIndexPaths
line, everything works fine for me.
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
if editingStyle == .Delete {
if let index: NSIndexPath = indexPath {
self.tableView.beginUpdates()
self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
self.configurations.delete(ProductDatabase.deleteProduct(self.products[index.row]))
loadProductsToListOrder()
self.tableView.endUpdates()
}
} else if editingStyle == .Insert {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
}
}
I searched and found here on stack overflow about this beginUpdates
and endUpdates
.
Thank you.
Update
loadProductsToListOrder function
func loadProductsToListOrder() {
self.products = ProductDatabase.listProduct(self.configurations.db!)
self.tableView.reloadData()
}
If I have one element in my TableView, I do not have this problem. Just happen if I have more than one itens in my TableView.
Update 2
The answer that @harshayarabarla gave works for me, because I was forgetting to add the self.products.removeAtIndex(indexPath.row)
before the deleteRowsAtIndex.
Thanks!