7

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.

ravoorinandan
  • 783
  • 2
  • 10
  • 26

4 Answers4

5

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.

zaph
  • 111,848
  • 21
  • 189
  • 228
  • 2
    but in numberofrowsinsection i am passing the value as 3, So how does it directly skips to 1 instead of starting from 0? – ravoorinandan Oct 21 '14 at 12:25
  • And to my surpise it is happening in iOS8 but not in iOS7. But i dont think that should be a issue, it is just a wild guess. – ravoorinandan Oct 21 '14 at 12:30
  • yes i do agree that Zaph, but the table view is capable of displaying three cells and the point is with out calling any method like reloadRowsatIndexpath what makes it to skip 0 and directly move to 1 when the table is capable of displaying all the three cells. – ravoorinandan Oct 21 '14 at 12:34
  • and as i mentioned earlier this issue is happening in ios8 device and not in ios7 :( – ravoorinandan Oct 21 '14 at 12:39
  • yes i was able to see only 2 rows in ios8 and 3 in ios7 for the same piece of code. – ravoorinandan Oct 21 '14 at 12:45
  • OK, now we have the problem. try adding an NSLog() of `numberOfRowsInSection:0` to the top of `tableView:cellForRowAtIndexPath`. – zaph Oct 21 '14 at 12:57
  • Yes i have added and it is printing it as 3, but there are only two rows visible – ravoorinandan Oct 21 '14 at 13:16
3

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.

Ben Resplendent
  • 408
  • 4
  • 12
0

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.

ManuQiao
  • 708
  • 8
  • 20
0

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 :)