0

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!!

Pratik Jamariya
  • 810
  • 1
  • 10
  • 35
Yikes
  • 87
  • 1
  • 11
  • Could you show the whole error message? – Larme Nov 15 '17 at 10:34
  • @Larme I just edited my question to include that! :) – Yikes Nov 15 '17 at 10:40
  • It's unclear, but `self.growler.removeMonster(monsterDude)` and `let monsterDude = monsterSections[indexPath.section].monsters[indexPath.row]`. You are removing the monster accessing through `growler.removeMonster` and `monsterSections` on the second one. Why two differents access? Do they point to the same one? What's your code in numberOfItemForSection? – Larme Nov 15 '17 at 10:43
  • Are you simply missing calls to `tableView.beginUpdates()` before calling remove monster and `tableView.endUpdates()` after the cell deletion? – Matic Oblak Nov 15 '17 at 10:46
  • @Larme growler is the reference to my class that actually has removeMonster. The monsterSections refers to a struct I made called MonsterSections. – Yikes Nov 15 '17 at 10:47
  • @MaticOblak I tried that and, unfortunately, it didn't work. – Yikes Nov 15 '17 at 10:49
  • @Larme Here is my numberOfItemSections: override func numberOfSections(in tableView: UITableView) -> Int { return monsterSections.count } – Yikes Nov 15 '17 at 10:49
  • Does `self.growler.monsters` have the same address of `self.monsters`? You can check this to print the address if needed (https://stackoverflow.com/questions/24058906/printing-a-variable-memory-address-in-swift) – Larme Nov 15 '17 at 10:52
  • @Larme growler: 0x7ffdb450b638 monsterSections: 0x7fcb9de0b570 I did get an error when I tried to run the code, though. "Simultaneous accesses to 0x7fcb9de0b570, but modification requires exclusive access" – Yikes Nov 15 '17 at 10:58
  • I think that they point on two different objects. So when you remove an object from `self.growler.monsters`, it's doesn't remove on `self.monsters`. Could you print them after `self.growler.removeMonster(monsterDude)`? – Larme Nov 15 '17 at 12:46
  • @Larme growler: 0x7fc3b4c10d88 monsterSections: 0x7fb3afd0b2e0 – Yikes Nov 15 '17 at 22:15
  • 1
    I meant the content. I really think that you have two different lists that are not synched, and I still don't understand why sometimes you use self.growler and half of the other times you don't. – Larme Nov 17 '17 at 10:35
  • @Larme You were 100% correct! It works completely now that I've realized that. Thank you so much for your help! – Yikes Nov 22 '17 at 09:44

0 Answers0