What is the correct way to use prepareForReuse?

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

You should reset only those attributes of the cell that are not related to the content, such as alpha, editing, and selection state.

but what about resetting individual property attributes like isHidden?

Assuming my cell has 2 labels where I should dump:

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

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

+6
ios uitableview swift
source share
3 answers

Do not 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:) .

+2
source share

Quoting from Apple 's own documentation :

For performance reasons, only cell attributes should be reset. non-content related , such as alpha, editing and selection status.

for example, if a cell was selected, you simply set it as unselected, if you changed the background color to something, then you simply return it to the default color.

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

This means that if you tried to set profile images in your contact list, you should not try to use nil images in prepareforreuse , you should set the images correctly in cellForRowAt , and if you did not find the image then you set its image to nil or the default image . Basically cellForRowAt should manage both expected and unexpected status.

Therefore, 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, it is recommended to use elements that are not related to the default content, as shown below:

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

This is the method proposed by Apple. But to be honest, I still think it's easier to drop everything inside cellForRowAt , as Matt said. Clean code is important, but it really won’t help you achieve this. But, as Connor said, the only time needed for this is if you need to cancel the image upload. See here for more details.

those. 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.

+18
source share

As stated in the documentation, you should use this method only for reset attributes that are not related to content. Regarding reselling text / row count .... of your shortcuts, you can do this from within tableView (_: cellForRowAt :) before setting a new value:

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { cell.label.text = "" //reseting the text cell.label.text = "New value" return cell } 

OR

you can use a more object-oriented approach and subclass UITableViewCell and define a method that configureCell () says to handle all the reset settings and the values ​​of the newly selected cells.

0
source share

All Articles