Invalid number of rows in section even though I delete in order

天涯浪子 提交于 2020-08-09 08:16:21

问题


I have a tableView in which the user can delete cells. This is in cellForRowAt in class WhishlistTableViewController: UITableViewController:

cell.deleteWishCallback = { [unowned self] deletedCell in
    print("delete with callback")
    guard let indexPath = tableView.indexPath(for: deletedCell) else { return }
    self.deleteWishDelegate?.deleteWish(indexPath, currentWish.wishCounter)
    self.wishData.remove(at: indexPath.row)
    tableView.deleteRows(at: [indexPath], with: .right)
    print("removed from tableView")
}

But I am still using the delegate to remove the Wish from the database and also from my main datasourceArray inside the ViewController so I can safe it to UserDefaults:

extension WishlistViewController: DeleteWishDelegate {
    func deleteWish(_ idx: IndexPath, _ wishCounter: Int){
        // remove the wish from the user's currently selected wishlist
        print("delete2")
        self.dataSourceArray[currentWishListIDX].wishes.remove(at: idx.row)
        if let defaults = UserDefaults(suiteName: UserDefaults.Keys.groupKey) {
            defaults.setDataSourceArray(data: self.dataSourceArray)
            defaults.synchronize()
        } else {
            print("error")
        }
        DataHandler.deleteWish(wishCounter, self.dataSourceArray[currentWishListIDX].name)
    }
}

However this causes an error if I delete cells and add again:

'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (5) 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 (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

I searched for that and this usually fire when you don't delete in the right order but I thought I am doing it the right way? (first from the tableViewData(wishData) and then from the tableView itself with deleteRows. What am I missing here?

By the way when deleting a cell this is the order of the print-statements:

delete with callback

delete2

number of rows: 2

removed from tableView

Update:

As suggested in the comments, the problem might be in how I add cells. This is how I do that:

func addWishComplete(wishName: String, selectedWishlistIDX: Int, wishImage: UIImage?, wishLink: String?, wishPrice: String?, wishNote: String?, wishCounter: Int?) {
    
    let wishToAdd = Wish(name: wishName, link: wishLink!, price: wishPrice!, note: wishNote!, image: wishImage!, checkedStatus: false, wishCounter: wishCounter!)
    
    self.dataSourceArray[selectedWishlistIDX].wishes.append(wishToAdd)
    DataHandler.saveWish(dataSourceArray: self.dataSourceArray, selectedWishlistIdx: selectedWishlistIDX, wish: wishToAdd)
    // save dataSourceArray with new wish in UserDefaults
    if let defaults = UserDefaults(suiteName: UserDefaults.Keys.groupKey) {
        defaults.setDataSourceArray(data: self.dataSourceArray)
        defaults.synchronize()
    } else {
        print("error wish to datasource")
    }

    // only update current list if selectedWishlist is currentWishlist
    if selectedWishlistIDX == currentWishListIDX {
        wishList.wishes.append(Wish(name: wishName, link: wishLink!, price: wishPrice!, note: wishNote!, image: wishImage!, checkedStatus: false, wishCounter: wishCounter!))
        
        theTableView.wishData = wishList.wishes
        theTableView.tableView.beginUpdates()
        theTableView.tableView.insertRows(at: [
            (NSIndexPath(row: theTableView.wishData.count-1, section: 0) as IndexPath)], with: .left)
        theTableView.tableView.endUpdates()
    }
    dismissWishView()
}

回答1:


I found the problem. As already mentioned in the comments I have to can work directly on wishData to remove :

func addWishComplete(wishName: String, selectedWishlistIDX: Int, wishImage: UIImage?, wishLink: String?, wishPrice: String?, wishNote: String?, wishCounter: Int?) {
    
    let wishToAdd = Wish(name: wishName, link: wishLink!, price: wishPrice!, note: wishNote!, image: wishImage!, checkedStatus: false, wishCounter: wishCounter!)
    
    self.dataSourceArray[selectedWishlistIDX].wishes.append(wishToAdd)
    DataHandler.saveWish(dataSourceArray: self.dataSourceArray, selectedWishlistIdx: selectedWishlistIDX, wish: wishToAdd)
    // save dataSourceArray with new wish in UserDefaults
    if let defaults = UserDefaults(suiteName: UserDefaults.Keys.groupKey) {
        defaults.setDataSourceArray(data: self.dataSourceArray)
        defaults.synchronize()
    } else {
        print("error wish to datasource")
    }

    // only update current list if selectedWishlist is currentWishlist
    if selectedWishlistIDX == currentWishListIDX {
        wishList.wishes.append(wishToAdd)
        
        theTableView.wishData.append(wishToAdd)
        theTableView.tableView.beginUpdates()
        theTableView.tableView.insertRows(at: [
            (NSIndexPath(row: theTableView.wishData.count-1, section: 0) as IndexPath)], with: .left)
        theTableView.tableView.endUpdates()
    }
    dismissWishView()
}

Thanks for all the help!



来源:https://stackoverflow.com/questions/63112938/invalid-number-of-rows-in-section-even-though-i-delete-in-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!