When is cellForRowAtIndexPath method called?

Deadly 提交于 2020-05-27 05:37:19

问题


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.


回答1:


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.




回答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.



回答3:


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.




回答4:


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

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




回答5:


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.




回答6:


-(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.




回答7:


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...



来源:https://stackoverflow.com/questions/36418359/when-is-cellforrowatindexpath-method-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!