CollectionView
にイメージの配列をロードしています。コレクションビューのインデックスがリフレッシャーを使用しているとき
私は補習を有効にすると、私はこのメソッドを呼び出しています:
func fetchPics(){
//Clear array of post
pics?.removeAll()
Common.sharedInstance.fetchPics(completion: { (pics) in
self.pics = pics
self.collectionView?.reloadData()
self.refresher.endRefreshing()
})
}
ビューコントローラのviewDidLoad
は罰金この方法と、すべての負荷を呼び出します。
fatal error: Index out of range
エラーに:しかし、復習からそれを呼び出すと、私に与え
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: postCellId, for: indexPath) as! PicCell
//This is the line that gives me the error
cell.pic = pics?[(indexPath as NSIndexPath).item]
return cell
}
このエラーを取得せずにコレクションビューを再ロードする適切な方法は何ですか?
はあなたの閉鎖コードは、後に実行されますので、あなたcollectionView
あなたは空の配列を取得しているcellForItemAt indexPath
を呼ぶかもしれない場合は、完了ブロック内でもpics?.removeAll()
を配置しようとすると
試し' cell.pic =写真をリードする指導のために君たちに感謝し、それは私が内部にそれを追加しようとした –