I needed to perform some actions with all visible cells, when the collection view is loaded before it becomes visible to the user, I used:
public func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) { if shouldPerformBatch { self.collectionView.performBatchUpdates(nil) { completed in self.modifyVisibleCells() } } }
Note that this will be called when scrolling through the collection view, so to prevent this, I added:
private var souldPerformAction: Bool = true
and in the action itself:
private func modifyVisibleCells() { if self.shouldPerformAction { // perform action ... ... } self.shouldPerformAction = false }
The action will continue to be performed several times, since the number of visible cells in the initial state. but in all these calls you will have the same number of visible cells (all of them). A logical flag will prevent it from re-launching after the user begins to interact with the collection view.
gutte May 6 '18 at 13:15 2018-05-06 13:15
source share