I have a UICollectionView
which displays cells that, in part, have images I need to get from a server. In cellForItemAt
, I check my cache to see if the image is available and if not I call a method to download the image.
In that method, I load the image asynchronously. When the image is downloaded, I check to see if the indexPath associated with that image is visible. If so I call reloadItems to update the display.
The problem is that I can see the empty cell on the simulator, but it is not in the array of visible cells.
Here is a minimal snippet that shows the problem.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: ThumbCell = collectionView.dequeueReusableCell(withReuseIdentifier: kReuseId, for: indexPath) as! PosterThumbCell
print("cellforItemAt: \(indexPath)")
print("visibleItems: \(collectionView.indexPathsForVisibleItems)")
...
return cell
}
Now I would expect the indexPath to be in the array of visible items. But it is not. Is there some event that has to occur before the item is considered visible? What am I missing?