iOS UITableView:"cellForRowAtIndexPath"和"cellForRowAtIndexPath"之间有什么区别?和"willDisplayCell:forRowAtIndexPath:"; [英] iOS UITableView: what's the different between "cellForRowAtIndexPath" and "willDisplayCell: forRowAtIndexPath:"
问题描述
就像问题的标题提到的那样:
Just as the question's title mentions:
What's the difference between "cellForRowAtIndexPath
" and "willDisplayCell
: forRowAtIndexPath
:"?`
我认为可以在cellForRowAtIndexPath
或willDisplayCell: forRowAtIndexPath:
中完成单元配置!
I think cell configuration can be done either in cellForRowAtIndexPath
or willDisplayCell: forRowAtIndexPath:
"!
推荐答案
是的,在两种方法中都可以完成单元格配置(理论上).
You are right, cell configuration can (in theory) be done in both methods.
但是,几乎所有UITableView
都有一个实现cellForRowAtIndexPath:
的数据源(这是协议中的必需方法).另一方面,willDisplayCell:forRowAtIndexPath:
(这是 delegate 的方法,而不是数据源)是可选的.
However, almost all UITableView
have a data source which implements cellForRowAtIndexPath:
(it is a required method in the protocol). On the other hand, the willDisplayCell:forRowAtIndexPath:
(which is a method of the delegate, not the data source) is optional.
由于配置单元通常取决于您要显示的数据,因此cellForRowAtIndexPath:
是迄今为止最常见的单元配置场所. (我什至不记得使用willDisplayCell:forRowAtIndexPath:
).
As configuring a cell is usually dependent on the data you want to show, cellForRowAtIndexPath:
is by far the most common place to do cell configuration. (I can't even remember using willDisplayCell:forRowAtIndexPath:
).
有一个显着的例外:使用情节提要和静态单元格(而不是单元格原型)时,在 cellForRowAtIndexPath:
中无法做任何有用的事情(因为dequeueReusableCellWithIdentifier:
返回nil
),因此您必须使用willDisplayCell:forRowAtIndexPath:
,viewWillAppear:
或其他方法进行配置.
There's one notable exception: when you are using a storyboard and static cells (instead of cell prototypes), you can't do anything useful in cellForRowAtIndexPath:
(because dequeueReusableCellWithIdentifier:
returns nil
), so you have to do configuration in willDisplayCell:forRowAtIndexPath:
, viewWillAppear:
or other methods.
@NSDeveloper:您是对的.感谢您的提示.
@NSDeveloper: you're right. Thanks for the hint.
这篇关于iOS UITableView:"cellForRowAtIndexPath"和"cellForRowAtIndexPath"之间有什么区别?和"willDisplayCell:forRowAtIndexPath:";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!