Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When is cellForRowAtIndexPath method called?

When exactly is this method called ? Suppose at a time I have 4 of my tableview cells visible on the screen then for how many cells will this method be called. Also, what if a cell is partially visible, will the method be called for the next cell as well in this case ?

P.S. I am not stuck anywhere, so showing some code doesn't make any sense. Its just a fundamental doubt.

like image 349
Pulkit Sharma Avatar asked Apr 05 '16 06:04

Pulkit Sharma


2 Answers

The most simple explanation I can give you is - cellForRowAtIndexPath, will call when a cell is about to be shown in the screen of your view controller. So this condition is valid whenever

  • Tableview is being reloaded [tableview reloadData], thus every cell will be dequeued/ populated again.
  • Every time you scroll and are about to see a new cell. UITableview uses the cell that is going out of the view (visible area), and uses that particular cell to populate new data.
  • After numberOfRows. If your View Controller has a tableview in the storyboard. This is called in viewDidLoad as when the VC inspects a tableview in the storyboard, it will call the mandatory tableview datasource numberOfRows first and then it will call cellForRowAtIndexpath, that amount of time, the number of cells visible in the screen.

Hope could solve your confusion.

like image 74
Saheb Roy Avatar answered Oct 02 '22 01:10

Saheb Roy


This method will called for the number of time for your number of cell visible.

Suppose current 4 cell visible(row0,row1,row2,row3). it call for 4 time.

when you scroll and now again visible 4 cell like (row2,row3,row4,row5). It again for 4 time for row 2...5. 

So this method will call for each visible cell in tableview every time when your scroll table.
like image 34
Yagnesh Dobariya Avatar answered Oct 01 '22 23:10

Yagnesh Dobariya