我有一个CollectionView
向用户显示图像。我在后台下载这些,下载完成后,我调用以下函数来更新collectionViewCell
并显示图像。
func handlePhotoDownloadCompletion(notification : NSNotification) {
let userInfo:Dictionary<String,String!> = notification.userInfo as! Dictionary<String,String!>
let id = userInfo["id"]
let index = users_cities.indexOf({$0.id == id})
if index != nil {
let indexPath = NSIndexPath(forRow: index!, inSection: 0)
let cell = followedCollectionView.cellForItemAtIndexPath(indexPath) as! FeaturedCitiesCollectionViewCell
if (users_cities[index!].image != nil) {
cell.backgroundImageView.image = users_cities[index!].image!
}
}
}
如果单元格当前在屏幕上可见,那么这非常有效,但是如果不是,我会得到一个fatal error: unexpectedly found nil while unwrapping an Optional value
以下行出现错误:
let cell = followedCollectionView.cellForItemAtIndexPath(indexPath) as! FeaturedCitiesCollectionViewCell
现在,如果 collectionViewCell 尚不可见,则甚至不需要调用此函数,因为在这种情况下,图像将被设置在cellForItemAtIndexPath
无论如何方法。
因此我的问题是,如何更改此函数来检查我们正在处理的单元格当前是否可见。我知道的collectionView.visibleCells()
但是,我不确定如何在这里应用它。
获取当前可用的单元格
// get visible cells
let visibleIndexPaths = followedCollectionView.indexPathsForVisibleItems
然后检查您的indexPath
包含在visibleIndexPaths
数组与否,在对单元格进行任何操作之前。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)