I'm building an app that has a table and I'm trying to delete rows. When this error first popped up, I thought it originated from my view controller. I looked on StackOverflow and I found similar questions to mine but NONE of the solutions worked. Here is that code:
override func tableView(_ tableView: UITableView,
commit editingStyle: UITableViewCellEditingStyle,
forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let monsterDude = monsterSections[indexPath.section].monsters[indexPath.row]
verifyDelete(monsterDude.name, {
(action) -> Void in
self.growler.removeMonster(monsterDude)
self.tableView.deleteRows(at: [indexPath],
with: .automatic)
})
}
}
I tried a bunch of different things - from testing with reloadData
to rewriting my numberOfSections
method - so now I think that it must be an issue with my removeMonster method itself (actually deleting the key and value from the dictionary I made to store all of my 'monsters'). Here is that class:
func removeMonster(_ name: String, _ monsterType: MonsterType, _ strength: Strength) {
let monsterMan = Monster(name, monsterType, strength)
removeMonster(monsterMan)
}
func removeMonster(_ monsterMan: Monster) {
monsters.removeValue(forKey: monsterMan.strength)
}
This is how I declare my dictionary:
var monsters = Dictionary<Strength, [Monster]>()
Here's the error message:
*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3698.21.8/UITableView.m:2011
2017-11-15 05:04:37.065738-0500 Project1[65062:17116869] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
Please let me know if there are any glaring errors that stick out to you! I'm happy to provide more info if necessary. I just want to delete things from my table. Thanks!!