Update UITableView with Data Fails

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!

Accepted Reply

Hi,


Thank you. I have fixed it. Its my silly numbering. The correct code should be:


...

for i in 0...tableData.count-1 {

let indexPath = IndexPath(row: i, section: 0)

indexPaths.append(indexPath)

}

...


the array needed to go from 0, to count-1 (as array objects start at 0 for the first item, so i should go from 0 to 4)

the indexpath should be inserted with the first one at 0, ie i, not i+1, as the first row is 0, the first section is 0.


I hope that is a good expanation and I hope it helps someone else.

When inserting row 5, there should be 6 rows in the datamodel, not 5, as the 5th indexPath is row 6.

Replies

It is difficult to say without seeing the complete code.


But usually, the way to do is;


- Create the dataSource (tabledata)

- define numberOfRowsInSection as you did


- call cellForRowAt

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { }

to populate cells with data source.


Did you do this one ?

Hi,


Thank you. I have fixed it. Its my silly numbering. The correct code should be:


...

for i in 0...tableData.count-1 {

let indexPath = IndexPath(row: i, section: 0)

indexPaths.append(indexPath)

}

...


the array needed to go from 0, to count-1 (as array objects start at 0 for the first item, so i should go from 0 to 4)

the indexpath should be inserted with the first one at 0, ie i, not i+1, as the first row is 0, the first section is 0.


I hope that is a good expanation and I hope it helps someone else.

When inserting row 5, there should be 6 rows in the datamodel, not 5, as the 5th indexPath is row 6.