Reputation: 911
When I call reloadData method of UICollectionView it cause all visible cell to blink. I found that reason of this is recreation of all visible cells, while UITableView return visible cell in – tableView:cellForRowAtIndexPath: method after reload data.
So, how can I avoid recreation of visible cells in UICollectionView.
Upvotes: 25
Views: 20437
Reputation: 312
Swift Version:
if let indexPaths = collectionView?.indexPathsForVisibleItems {
collectionView?.reloadItems(at: indexPaths)
}
Upvotes: 4
Reputation: 411
You can simply create your own flow layout and set alpha to initial layout attribute, e.g.
class MyFlowLayout: UICollectionViewFlowLayout {
override func initialLayoutAttributesForAppearingItem(at itemIndexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
let attr = layoutAttributesForItem(at: itemIndexPath)
attr?.alpha = 1
return attr
}
}
It's necessary because by default alpha is 0, and if cells are re-created - you see blinking.
Upvotes: 4
Reputation: 476
You need to reload the visible cells, which will not cause the flash seen during reloadData
:
[myCollectionView reloadItemsAtIndexPaths:[myCollectionView indexPathsForVisibleItems]];
Upvotes: 19
Reputation: 3615
You can try https://github.com/ba01ei/PSTImprovedCollectionView
With this collection view, when you call reloadData, if a cell is still visible, the same cell will be returned from the queue (then you can check the data model attached to the cell and decide whether to re-render)
Upvotes: 1
Reputation: 119041
Calling reloadData
on either a table or a collection view will recreate (dequeue if possible) all of the (visible) cells and refresh the information about the row / item count and the view content height. So, you will always see a 'blink' because it's animated.
This is by design to inform the user that everything was just updated...
To avoid animation, don't call reloadData
. Instead, use visibleCells
to get all of the cells that currently exist on the screen and update their contents directly.
Upvotes: 7