indexpath.row is starting from 1 instead of 0

2019-04-27 23:44发布

问题:

i am facing a strance issue with a uitableview. sometime when my table's datasource/numberofrowsinsection has 3 values, cellforowatindexpath starts from 0-1 instead of 0-0. can someone help me out finding the possible reasons for this issue to happen. even tried printing the logs but the logs were shown for 0 -1 and 0 -2 but not for 0 -0. That means it is not getting called for the first row i.e 0 -0.

回答1:

tableView:cellForRowAtIndexPath: is called as needed to display a cell, if a cell is not going to be displayed it is not called. If row 0 is not being displayed section-cell 0-0 will not be called.



回答2:

Like Zaph stated, tableView:cellForRowAtIndexPath: is called whenever a table wants to display a cell for a given index path, so if you're not getting an index path with a row of 0 ever, it means you're likely doing something that prevents that cell from getting shown.

My first suggestion would be to investigate your tableView:heightForRowAtIndexPath: method, and 1) check if it's getting called with an index path row of 0, and 2) make sure something > 0 is being returned.

The fact that you're seeing it on iOS 8 only does not necessarily mean something native is broken, but might instead suggest that some of your logic which contributes to the above methods is affected by iOS 8 changes. iOS 8 had thrown off the way I was doing text size calculations, so perhaps something less obvious like that is resulting in returning a height not greater than 0.



回答3:

You may first check number of sections and number of rows. If row number of a section returned 0, that section will not be called.



回答4:

The thing that worked in my case is I have written method estimatedHeightForRowAt method in my class. When I commented that method, the code is working fine..

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {

}

I have removed the above method from my code. It saved my day :)