What is the correct way to use prepareForReuse?

我的未来我决定 提交于 2019-11-27 01:42:02

问题


Need help with understanding how to use prepareForReuse() in UIKit. The documentation says

you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state

but what about resetting individual property attributes such as isHidden?

Assuming my cell has 2 labels where should I reset:

  1. label.text
  2. label.numberOfLines
  3. label.isHidden

My tableView(_:cellForRowAt:) delegate has conditional logic to hide/show labels per cell.


回答1:


Quoting from Apple's own documentation:

For performance reasons, you should only reset attributes of the cell that are not related to content, for example, alpha, editing, and selection state.

e.g. if a cell was selected, you just set it to unselected, if you changed the background color to something then you just reset it back to its default color.

The table view's delegate in tableView(_:cellForRowAt:) should always reset all content when reusing a cell.

This means if you were trying to set the profile images of your contact list you shouldn't attempt to nil images in prepareforreuse, you should correctly set your images in the cellForRowAt and if you didn't find any image then you set its image to nil or a default image. Basically the cellForRowAt should govern both the expected/unexpected status.

So basically the following is not suggested:

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

instead the following is recommended:

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
}

Additionally default non-content related items as below is recommended:

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

}

This is the Apple's suggested way. But to be honest, I still think it's easier to dump everything inside cellForRowAt just like Matt said. Clean code is important, but this may not really help you achieve that. But as Connor said the only time it's necessary is, if you need to cancel an image that is loading. For more see here

ie do something like:

override func prepareForReuse() {
    super.prepareForReuse()

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

Additionally in the special case of using RxSwift: See here or here




回答2:


As stated by the documentation you only have to use the said method to reset attributes that are not related to the content. As for reseting the text/number of lines.... of your labels you could do it from within tableView(_:cellForRowAt:) just before you set their new value, like so:

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

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

OR

you could take a more object oriented approach and create a subclass of UITableViewCell and define a method say configureCell() to deal with all the resetting and value setting of newly dequeued cells.



来源:https://stackoverflow.com/questions/40773208/what-is-the-correct-way-to-use-prepareforreuse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!