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
Value of type ‘UICollectionViewCell?’ has no member ‘imgAttachment’
解决方法
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 }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。