使用prepareForReuse的正确方法是什么?

时间:2016-11-23 20:01:35

标签: ios swift uitableview

需要帮助了解如何在UIKit中使用prepareForReuse()。 documentation

  

您应该只重置与之无关的单元格的属性   内容,例如,alpha,编辑和选择状态

但是重置个别属性属性如isHidden呢?

假设我的手机有2个标签我应该重置:

  1. label.text
  2. label.numberOfLines
  3. label.isHidden
  4. 我的tableView(_:cellForRowAt :)委托有条件逻辑来隐藏/显示每个单元格的标签。

3 个答案:

答案 0 :(得分:10)

引用Apple自己的documentation

  

出于性能原因,您应该只重置单元格的属性   与内容无关,例如,alpha,编辑和   选择状态。

e.g。如果选择了一个单元格,您只需将其设置为未选中,如果您将背景颜色更改为某些内容,则只需将其重置为默认颜色。

  

tableView(_:cellForRowAt:) 中的表格视图的委托应该是   重复使用单元格时,请始终重置所有内容

这意味着如果您尝试设置联系人列表的个人资料图片,则不应在nil中尝试prepareforreuse张图片,您应该在cellForRowAt中正确设置图片如果您没有找到任何图像然后,则将其图像设置为nil或默认图像。基本上cellForRowAt应该控制预期/意外状态。

所以基本上没有建议以下内容:

override func prepareForReuse() {
    super.prepareForReuse()
    imageView?.image = nil
}

而是推荐以下内容:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)

     cell.imageView?.image = image ?? defaultImage // unexpected situation is also handled. 
     // We could also avoid coalescing the `nil` and just let it stay `nil`
     cell.label = yourText
     cell.numberOfLines = yourDesiredNumberOfLines

    return cell
}

此外,建议使用以下默认的非内容相关项目:

override func prepareForReuse() {
    super.prepareForReuse()
    isHidden = false
    isSelected = false
    isHighlighted = false

}

这是Apple建议的方式。但说实话,我仍然认为将所有内容转移到cellForRowAt内更容易,就像马特所说的那样。清洁代码很重要,但这可能无法帮助您实现这一目标。但是Connor said唯一需要的时候是here,如果你需要取消正在加载的图像。有关详情,请参阅pprof

即做类似的事情:

override func prepareForReuse() {
    super.prepareForReuse()

    imageView.cancelImageRequest() // this should send a message to your download handler and have it cancelled.
    imageView.image = nil
}

答案 1 :(得分:3)

根本不使用prepareForReuse。它存在,但很少有情况下它是有用的,而你的不是其中之一。完成tableView(_:cellForRowAt:)中的所有工作。

答案 2 :(得分:0)

如文档所述,您只需使用上述方法重置与内容无关的属性。至于重新设置标签的文本/行数....你可以在设置新值之前从tableView(_:cellForRowAt :)中执行此操作,如下所示:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    cell.label.text = "" //reseting the text
    cell.label.text = "New value"
    return cell
    }

OR

您可以采用更面向对象的方法并创建UITableViewCell的子类,并定义一个方法,即configureCell()来处理新出列单元格的所有重置和值设置。

相关问题