0

I'm trying to programatically insert a new row between two existing rows in my static UITableView, but it crashes saying:

'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (6) must be equal to the number of rows contained in that section before the update (6), 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).'

This is the code I'm using to add the new row

if (indexPath.section == 1 && indexPath.row == 3) {
    // If this is the first subtask we want it to appear under Sub Tasks
    if (subTaskRow.count == 0) {
         tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: 5, inSection: 1)], withRowAnimation: .Automatic)
        // Added the first subtask
        subTaskRow.addObject(5)
        tableView.reloadData()
    // Other sub tasks should appear below previous sub tasks
    } else {
        let row = (subTaskRow.lastObject as! Int) + 1
        tableView.insertRowsAtIndexPaths([NSIndexPath(forRow: row, inSection: 1)], withRowAnimation: .Automatic)
        // Keep track of subsequent subtasks
        subTaskRow.addObject(row)
        tableView.reloadData()
    }
}
Julian E.
  • 4,687
  • 6
  • 32
  • 49
Chris Byatt
  • 3,689
  • 3
  • 34
  • 61
  • have you tried moving the `subTaskRow.addObject(row)` before the `insertRowsAtIndexPaths`!? – luk2302 Jun 14 '15 at 19:17
  • @luk2302 that won't make a difference - subTaskRow is just an array to keep track of the indexPaths of the rows i've added – Chris Byatt Jun 14 '15 at 19:19
  • i disagree, in your implementation of `numberOfRowsInSection`, what do you return? `subTaskRow.count`maybe!? – luk2302 Jun 14 '15 at 19:20
  • @luk2302 no. it's a static table view. I've not implemented that function at all. – Chris Byatt Jun 14 '15 at 19:20
  • aaaaah, static tableViews, that is a useful information - take a look at http://stackoverflow.com/questions/10043521/adding-unknown-number-of-rows-to-static-cells-uitableview you can not *just* insert new rows. – luk2302 Jun 14 '15 at 19:22
  • @luk2302 i'll check it out thanks. I did mention it was static in the title :P – Chris Byatt Jun 14 '15 at 19:23
  • okay, in the title... :D I was more focused on the body of the question ;) – luk2302 Jun 14 '15 at 19:23

0 Answers0