2

I have an array I need to add and remove cell data on click of "close" which is UIButton in header section

I am looping the array and getting row and for section I have used static data and appending in indexPaths then after

tblSideNav.deleteRows(at: indexPaths, with: .fade) 

as above line start executing app got crash

 var menusArray = [sideNavStruct(isOpend: true, menuImg: "user", menuTitle: "Profile", subMenu: ["My Profile", "Distributor Profile"]), sideNavStruct(isOpend: true, menuImg: "user", menuTitle: "Reports", subMenu: ["Stock Report", "NFR Report", "RTD”])]

    @objc func handleOpenCloseCell() {
    let section = 0
    var indexPaths = [IndexPath]()
    for row in menusArray[section].subMenu.indices {
        let indexPath = IndexPath(row: row, section: section)
        indexPaths.append(indexPath)
    }
    print(indexPaths.count)
    print(menusArray[section].subMenu[0])
    menusArray[section].subMenu[0].removeAll()
    tblSideNav.deleteRows(at: indexPaths, with: .fade)
}

my app got crash on tblSideNav.deleteRows(at: indexPaths, with: .fade):

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

I am not able to find the issue

Rizwan Mehboob
  • 1,333
  • 17
  • 19
Sanjay Mishra
  • 672
  • 7
  • 17
  • 2
    What does the crash says? – Ahmad F Feb 17 '19 at 08:35
  • 1
    Labwa @AhmadF ... – Mohmmad S Feb 17 '19 at 08:54
  • 1
    @AhmadF reason: 'Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (7) 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, 2 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).' – Sanjay Mishra Feb 17 '19 at 09:27

1 Answers1

0

First of all, you are calling deleteRows without beginUpdates and endUpdates which is required.

tableView.beginUpdates()
tableView.deleteRows(at: indexPaths, with: .fade)
tableView.endUpdates()

The same error can occur if your delegate function is not returning the correct number of rows after the update.

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return rowCount
}

I believe that better practice would be to change/empty the variable where you're storing your rows information/data and simply call reloadData().

  • @SanjayMishra Due to the error message i think you are really returning wrong number of rows in the NumberOfRowsInsection delegate function. You should check on that. I can take closer look to your code if you can provide more context. – Matěj Simota Feb 17 '19 at 12:37
  • @Mateh Simota ti return numberOfRowInSection I used below code can you check it once I am using right or wrong. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if menusArray[section].isOpend == true { print(menusArray[section].subMenu.count) if menusArray[section].subMenu.count > 0 { return menusArray[section].subMenu.count } else { return menusArray[section].menuTitle.count } } else { return 0 } } – Sanjay Mishra Feb 17 '19 at 12:42
  • @SanjayMishra I see the problem now... when subMenu.count == 0, you are returning "return menusArray[section].menuTitle.count" ,which means that you return number of letters of your title. just delete the whole inner if statement. if menusArray[section].isOpend == true { return menusArray[section].subMenu.count } else { return 0 } – Matěj Simota Feb 17 '19 at 13:07
  • 1
    @Mates Simota now it is giving error 'NSInternalInconsistencyException', reason: 'attempt to delete row 1 from section 0 which only contains 0 rows before the update' – Sanjay Mishra Feb 17 '19 at 13:24