Check whether cell at indexPath is visible on screen UICollectionView

后端 未结 4 863
春和景丽
春和景丽 2021-02-19 18:44

I have a CollectionView which displays images to the user. I download these in the background, and when the download is complete I call the following func to update

4条回答
  •  春和景丽
    2021-02-19 19:32

    You can simply use if collectionView.cellForItem(at: indexPath) == nil { }. The collectionView will only return a cell if it is visible.

    Or in your case specifically change:

    let cell = followedCollectionView.cellForItemAtIndexPath(indexPath) as! FeaturedCitiesCollectionViewCell
    

    to:

    if let cell = followedCollectionView.cellForItemAtIndexPath(indexPath) as? FeaturedCitiesCollectionViewCell { }
    

提交回复
热议问题