0
我正在使用Firebase上的图像填充集合视图。一切工作正常,但是当我尝试执行赛格瑞我得到“意外发现零而展开的可选价值”这条线:UICollectionView单元格为零
if let indexPath = self.collectionView?.indexPath(for: sender as! UICollectionViewCell){}
我在所以在这里看到了很多工作的例子,显然他们都很好地工作那条线。
下面是相关代码的其余部分:
//grab Firebase objects in viewdidload and put them into productsArray
var productsArray = [ProductsModel]()
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return productsArray.count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
let imageView = cell.viewWithTag(1) as! UIImageView
//imageView.image = imageArray[indexPath.row]
let getUrl = productsArray[indexPath.row].productImg
imageView.loadUsingCache(getUrl!)
imageView.layer.borderColor = UIColor.lightGray.cgColor
imageView.layer.borderWidth = 1
imageView.layer.cornerRadius = 0
return cell
}
//NAVIGATION
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "itemSegue", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "itemSegue"){
let destinationController = segue.destination as! ItemViewController
if let indexPath = self.collectionView?.indexPath(for: sender as! UICollectionViewCell){
destinationController.getProduct = productsArray[indexPath.row]
}
}
}
另外,我仔细检查了一切连接,并在故事板设置。
在此先感谢!
感谢解释,无法相信我错过了。 – Noah
没有不发送单元作为发件人,你想indexPath,所以只需发送indexPath作为发件人。 –
@ Noah1尼拉夫说得很好。既然你正在寻找'indexPath',只需将'indexPath'作为'sender'而不是单元格。 –