如果与单元格关联的文件位于设备上,我试图添加图像并将其添加到collectionView单元格。如果文件存在,将图像添加到collectionView单元
该文件在那里,所以下面的代码取消隐藏图像,但是我得到一个错误,它发现零试图展开可选。
什么是错的代码什么想法?
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell: JourneyCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! JourneyCollectionViewCell
// query if file is on LDS and add image to indicate
let cellPartName = self.partArray[indexPath.item].name
let checkQuery = PFQuery(className: "downloadedAudio")
checkQuery.whereKeyExists(cellPartName)
checkQuery.fromLocalDatastore()
checkQuery.getFirstObjectInBackground(block: { (object, error) in
if error != nil || object == nil {
print("The file does not exist locally on the device, hide the image.")
//cell.ImageDownloaded.image = UIImage(named: "")
// crashes on this line
cell.ImageDownloaded.isHidden = true
} else {
print("the file already exists on the device, show the image.")
//cell.ImageDownloaded.image = UIImage(named: "download")
// crashes on this line
cell.ImageDownloaded.isHidden = false
}
})
return cell
}
the file already exists on the device, show the image.
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
图像 “下载” 是磁带。
'cell'的定义在哪里? – BallpointBen
崩溃说什么?大部分细节都是有用的。 – raidfive
我似乎没有在您的方法中定义任何'cell'变量。你忘记了离队吗? 'let cell = collectionView.dequeueReusableCell(withReuseIdentifier:reuseIdentifier,for:indexPath);' – ilbesculpi