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} 总结
以上是内存溢出为你收集整理的swift – 无法使用全局变量访问collectionView单元格变量全部内容,希望文章能够帮你解决swift – 无法使用全局变量访问collectionView单元格变量所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)