使用prepareForReuse的正确方法是什么? [英] What is the correct way to use prepareForReuse?
问题描述
需要帮助了解如何在UIKit中使用prepareForReuse()。
文档说
Need help with understanding how to use prepareForReuse() in UIKit. The documentation says
您应该只重置与
内容无关的单元格属性,例如,alpha,编辑和选择状态
you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state
但重置个别属性属性如isHidden呢?
but what about resetting individual property attributes such as isHidden?
假设我的单元格有2个标签我应该重置:
Assuming my cell has 2 labels where should I reset:
- label.text
- label.numberOfLines
- label.isHidden
我的tableView(_:cellForRowAt :)委托有条件逻辑来隐藏/显示每个单元格的标签。
My tableView(_:cellForRowAt:) delegate has conditional logic to hide/show labels per cell.
推荐答案
根本不要使用 prepareForReuse
。它存在,但很少有情况下它是有用的,而你的不是其中之一。在 tableView(_:cellForRowAt:)
中完成所有工作。
Don't use prepareForReuse
at all. It exists, but there are very few situations where it is useful, and yours is not one of them. Do all the work in tableView(_:cellForRowAt:)
.
这篇关于使用prepareForReuse的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!