Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 索引数组超出collectionviewcells的默认数量范围_Swift_Uicollectionviewcell - Fatal编程技术网

Swift 索引数组超出collectionviewcells的默认数量范围

Swift 索引数组超出collectionviewcells的默认数量范围,swift,uicollectionviewcell,Swift,Uicollectionviewcell,我正在与这个代码斗争 if facebookPhotos.count != 0 { if let data = NSData(contentsOfURL: facebookPhotos[indexPath.row]) { let image = UIImage(data: data) collectionCell.collectionViewImage.image = image } } 我有一个facebookphot

我正在与这个代码斗争

if facebookPhotos.count != 0 {         
    if let data = NSData(contentsOfURL: facebookPhotos[indexPath.row]) {    
        let image = UIImage(data: data)
        collectionCell.collectionViewImage.image = image
    }
}
我有一个facebookphotos数组,UICollectionView=>默认返回6个单元格

当我尝试将照片加载到它时(比如3),它崩溃了。如何使6个单元格显示,但仅显示那些已填充的单元格,这些单元格在indexPath.row处具有相同的照片


但拥有这6个单元格很重要。

避免索引数组超出范围异常很简单:您只需将
facebookPhotos.count
indexPath.row
进行比较即可:

if facebookPhotos.count < indexPath.row {         
    if let data = NSData(contentsOfURL: facebookPhotos[indexPath.row]) {    
        let image = UIImage(data: data)
        collectionCell.collectionViewImage.image = image
    }
} else {
    // Clear out the image on a recycled cell
    collectionCell.collectionViewImage = nil
}
如果facebookPhotos.count
让我开心。谢谢!出于某种原因,我又犯了同样的错误。知道为什么吗?@jvs它要么来自代码中的另一个地方,要么有一个线程同时修改您的数组。