I posted about this at the start of the month but nobody responded and I tried to alter it myself as days went by but after reading like 20 posts and trying to implement code from each post, I'm still stumped.
All I want do is successfully delete a cell in my app with no error.
This is the error I keep getting when trying to delete a cell, I've read it a million times, and tried to change up my code so many times, but I can't take it anymore.
Here is my code:
func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath, for orientation: SwipeActionsOrientation) -> [SwipeAction]? {
guard orientation == .right else { return nil }
let deleteAction = SwipeAction(style: .destructive, title: "Delete") { action, indexPath in
let documentId = self.documentsID[indexPath.row]!.docID
self.db.document("school_users/\(self.user?.uid)/events/\(documentId)").delete { (err) in
if let err = err {
print("Error deleting the docs")
} else {
print("Docs successfully deleted.")
}
}
self.documentsID.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .fade)
}
deleteAction.image = UIImage(named: "delete-icon")
return [deleteAction]
}
This function deletes the document out of the database which is great, but it also completely crashes my app, I've tried wrapping things in DispatchQueue.main
closures, DispatchQueue.global
closures and to all no avail. Yes this is an embarrassing question to ask but I just want to figure it out once and not have to worry about it again.
I will also attach my numberOfRowsInSection
method since the error mentions that as well.
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if events.count > 0 {
tableView.separatorStyle = .singleLine
tableView.separatorColor = .systemGreen
tableView.separatorInset = UIEdgeInsets(top: 0, left: 25, bottom: 0, right: 25)
return events.count
} else {
tableView.separatorStyle = .none
return 0
}
}
Thanks.
That error message is basically saying "You had 12 rows initially and you deleted 1 (with the
deleteRows
method`), but now are saying you still need 12 rows, so something's not right".So in your case, event.count must still be generating the number 12. Did you make sure to delete an element from
events
before calling thedeleteRows
method?