Hi All,
I am not sure what I am doing wrong, but its sending me a little crazy. I have a UITableView which I want to update with some rows. I wrote some code (and changed it a few times!) and have ended up with this, but I get a strange error.
The code is:
print("Rows in array: \(tableData.count)")
var indexPaths: [IndexPath] = Array()
for i in 0...tableData.count-1 {
let indexPath = IndexPath(row: i+1, section: 0)
indexPaths.append(indexPath)
}
print("Inserting table rows:\(indexPaths.count)")
tableView.insertRows(at: indexPaths, with: .automatic)
The first print line in the terminal shows: Rows in array: 5
The second one shows:Counting tabledata rows: 5
My numberOfRowsInSection looks like:
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
print("Counting tabledata rows: \(tableData.count)")
return tableData.count
}
and it prints to the terminal: Counting tabledata rows: 5
So the datasource has 5 rows, the array to add has 5 rows and the numberRowsInSection returns 5 and i get the error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'attempt to insert row 5 into section 0, but there are only 5 rows in section 0 after the update'
But surely the error is saying that the rows in section 0 ie 5 match the rows but after the update? Could someone explain where I have gone wrong and I will try to fix it!
TIA!