大佬教程收集整理的这篇文章主要介绍了swift – 无法使用全局变量访问collectionView单元格变量,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { var cell: UICollectionViewCell! if collectionView == collectionView1 { cell = collectionView.dequeueReusableCell(withReusEIDentifier: "cellAttachment",for: indexPath) as! AttachmentCell cell.imgAttachment.image = imageArray1[indexPath.row] cell.delegate = self } else if collectionView == collectionView2 { cell = collectionView.dequeueReusableCell(withReusEIDentifier: "cellAttachmentView",for: indexPath) as! AttachmentViewCell cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon } return cell
var cell: UICollectionViewCell!
您已将该单元格声明为UICollectionViewCell类型.因此,无论您在其中存储哪个子类,您的单元格将只是UICollectionViewCell类型.
你应该这样改变,
func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { if collectionView === collectionView1 { let cell = collectionView.dequeueReusableCell(withReusEIDentifier: "cellAttachment",for: indexPath) as! AttachmentCell cell.imgAttachment.image = imageArray1[indexPath.row] cell.delegate = self return cell } else if collectionView === collectionView2 { let cell = collectionView.dequeueReusableCell(withReusEIDentifier: "cellAttachmentView",for: indexPath) as! AttachmentViewCell cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon return cell } else { // Return the proper cell for other cases } }
或者,如果你是坚定的,你只需要在委托结束时只有一个return语句,那么你可以这样做,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { var yourCell: UICollectionViewCell! if collectionView === collectionView1 { let cell = collectionView.dequeueReusableCell(withReusEIDentifier: "cellAttachment",for: indexPath) as! AttachmentCell cell.imgAttachment.image = imageArray1[indexPath.row] cell.delegate = self yourCell = cell } else if collectionView === collectionView2 { let cell = collectionView.dequeueReusableCell(withReusEIDentifier: "cellAttachmentView",for: indexPath) as! AttachmentViewCell cell.imgFileIcon.image = imgArray2[indexPath.row].fileIcon yourCell = cell } else { // Return the proper cell for other cases } return yourCell }
以上是大佬教程为你收集整理的swift – 无法使用全局变量访问collectionView单元格变量全部内容,希望文章能够帮你解决swift – 无法使用全局变量访问collectionView单元格变量所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。