iOS UITableView:“cellForRowAtIndexPath"和“cellForRowAtIndexPath"有什么不同?和“willDisplayCell:forRowAtIndexPath:" [英] iOS UITableView: what's the different between "cellForRowAtIndexPath" and "willDisplayCell: forRowAtIndexPath:"

查看:31
本文介绍了iOS UITableView:“cellForRowAtIndexPath"和“cellForRowAtIndexPath"有什么不同?和“willDisplayCell:forRowAtIndexPath:"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如问题的标题所提到的:"cellForRowAtIndexPath" 和 "willDisplayCell: forRowAtIndexPath:" 有什么区别?`

Just as the question's title mentions: What's the difference between "cellForRowAtIndexPath" and "willDisplayCell: forRowAtIndexPath:"?`

我认为可以在 cellForRowAtIndexPathwillDisplayCell: 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:(这是委托的一个方法,不是数据源)是可选的.

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆