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()
}
}