【发布时间】:2020-04-14 09:03:51
【问题描述】:
我是 iOS 编程领域的新手,最近我在网上看到了一些实现的代码示例,例如:
functableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCell(withIdentifier: customCellIdentifier, for: indexPath) as? CustomCell
if (cell == nil) {
cell = CustomCell(style: UITableViewCell.CellStyle.default, reuseIdentifier: customCellIdentifier) asCustomCell
}
...
}
作者尝试处理dequeueReusableCell返回nil的事件。
但根据我对 UITableView 和自定义单元格的有限个人经验,我还没有遇到过 dequeueReusableCell 给我返回 nil 的情况。
通过研究,我发现原因可能是
“出队…方法试图找到一个具有给定重用的单元格 当前不在屏幕上的标识符。如果他们找到了,他们会返回 那个单元格,否则返回 nil。”
但这对我来说从来没有发生过一次。当我故意给它一个错误的标识符时,会发生运行时错误,但一次都没有返回 nil。我想知道这到底什么时候会发生,是否真的有必要进行处理。
【问题讨论】:
标签: ios swift uitableview