使用prepareForReuse的正确方法是什么? [英] What is the correct way to use prepareForReuse?

查看:1940
本文介绍了使用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:


  1. label.text

  2. label.numberOfLines

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

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