Cocoa Touch UITableView
s and UICollectionView
s reuse their cells for performance purposes, but this can cause strange behavior when loading data asynchronously. Loading images is the most common case. Images can show up in the wrong cells, either because they aren’t cleared out when the cell is reused, or because when the asynchronous call returns the cell is now being used somewhere else.
Big Nerd Ranch’s [iOS Programming] guide recommends a pattern to prevent this problem:
UITableViewCell
or UICollectionViewCell
and override prepareForReuse()
to clear out any data in the cell. (Reference for the UITableViewCell
version and UICollectionViewCell
version.)willDisplay
method is called. (tableView(_:willDisplay:forRowAt:)
and collectionView(_:willDisplay:forItemAt:)
)IndexPath
is being shown, and if so, what instance to use for it. If a cell is returned, the data can safely be populated there. (UITableView
’s cellForRow(at:)
method and UICollectionView
’s cellForItem(at:)
method) (Note that if the data can change, you may need to look up the correct IndexPath
as well, based on some other identifier.)