Reputation: 143
I'm following a tutorial on learning to code using Swift. In it I'm asked to create an array of arrays which is used to create a grouped table.
e.g.
var emojis = [[emojiarray1],[emojiarray2],[emojiarray3]]
A later exercise asks me to incorporate code so that a swipe will remove a row from the table view. Here is what I have inside the function that helps carry out this process:
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
emojis.remove(at: indexPath.row)
tableView.deleteRows(at: [indexPath], with: .automatic)
}
However, when I swipe to remove a row, instead of the row deleting I get an app crash with the message : Thread 1: Signal SIGABRT
And in the console I get
*** Assertion failure in -[UITableView _Bug_Detected_In_Client_Of_UITableView_Invalid_Number_Of_Sections:]
*** 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 (7) must be equal to the number of sections contained in the table view before the update (8), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
It says 'invalid number of sections', but I haven't removed a section and only a row in a section. I am thinking that maybe the emojis.remove(at: indexPath.row)
line may have removed a section, but I don't really know if thats true, or how to go about fixing it.
Upvotes: 1
Views: 225
Reputation: 100533
You delete a row from the table but remove a section from the array , you may need
emojis[indexPath.section].remove(at: indexPath.row)
Upvotes: 1