5

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.

Pulkit Sharma
  • 545
  • 1
  • 6
  • 19
  • 1
    The `UITableViewDataSource` methods cannot be strictly traced w.r.t their execution order. The `cellForRowAtIndexPath` method is called for all the visible rows. this include the partially visible row also. When you scroll the `UITableView` the method is again invoked to update the new rows. – Mathews Apr 05 '16 at 06:11

7 Answers7

7

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.

Saheb Roy
  • 5,899
  • 3
  • 23
  • 35
2
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.
Yagnesh Dobariya
  • 2,241
  • 19
  • 29
0

This method will be called whenever tableview display the cell.

e.g. if table display 4 cell then cellForRowAtIndexPath will called 4 times, and as you scroll the tableView this method will called once again.

cellForRowAtIndexPath will called whenever cell will be created or reuse.

Jatin Chauhan
  • 1,107
  • 1
  • 8
  • 21
0

Every time when your cell creates cellForRowAtIndexPath method will be called.

Suppose u have 4 cells then cellForRowAtIndexPath method will call 4 times.

Govind Prajapati
  • 957
  • 7
  • 24
0

The cellForRowAtIndexPath method will be called when you reload tableView and will be called the time for how many rows you return in numberOfRowsInSection method.

0
-(UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath

This is a datasorurce method of tableView. This is called for all the visible cells initially, though cell is partially visible it will be called.

Consider if you have 10 cells are 5 are visible. Firstly it will be called for 5 cells, and as you scroll down for other cells it will be called for next visible cells.

you can refer for more details.

Community
  • 1
  • 1
Chetan
  • 2,004
  • 1
  • 13
  • 21
-1

This Method will called whenever the cell get hit...

i.e.if u have 5 cell will called 5 times the method get hit and while scroll the tableview it will again hit based on the Resuse cell identifier Method...

fathima
  • 181
  • 12