如果與單元格關聯的文件位於設備上,我試圖添加圖像並將其添加到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