【发布时间】:2023-03-24 16:26:01
【问题描述】:
我正在将一组图像加载到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
}
在不出现此错误的情况下重新加载集合视图的适当方法是什么?
谢谢
【问题讨论】:
-
试试
cell.pic = pics?[indexPath.row]看看是否有效
标签: ios arrays swift swift3 collectionview