私はCollectionView
を持っています。これらをバックグラウンドでダウンロードし、ダウンロードが完了したら、次のfuncを呼び出してcollectionViewCell
を更新し、画像を表示します。indexPathのセルが画面上に表示されているかどうか確認してください。UICollectionView
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
今、この機能もする必要はありませんがこの場合、画像はcellForItemAtIndexPath
メソッドで設定されるため、collectionViewCellがまだ表示されていない場合に呼び出されます。
私の質問ですが、私が扱っているセルが現在表示されているかどうかを調べるためにこの関数を変更するにはどうすればよいですか?私はcollectionView.visibleCells()
を知っていますが、ここでそれをどのように適用するかはわかりません。可視セルvisibleIndexPaths
を取得し、細胞を用いて何かを行う前に、あなたのindexPath
はvisibleIndexPaths
に含まれる、またはされていませんチェックするfollowedCollectionView.indexPathsForVisibleItems()
を使用し