【发布时间】:2019-02-17 01:34:24
【问题描述】:
我有几个通过这种方法应用的自定义单元格
switch indexPath.row {
case 1:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "randomCell", for: indexPath)
as? randomCollectionViewCell
case 2:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "timeCell", for: indexPath)
as? timeCollectionViewCell
case 3:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ownerCell", for: indexPath)
as? ownerCollectionViewCell
default:
cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath)
as? imageModelCollectionViewCell
}
return cell
}
所有单元格同时按顺序显示。默认函数中的最后一个单元格是我需要从模型中传递值的 imageView。
模型将图片创建为链接,所以还需要上传图片。
比如这段代码就喜欢
cell.Image Model.image = ...
抛出错误
Value of type 'UICollectionViewCell?'has no member 'modelimage'
这是来自 collectionViewCell 的代码,用于传递数据
import UIKit
class imageModelCollectionViewCell: UICollectionViewCell {
@IBOutlet weak var modelImage: UIImageView!
}
如何将数据从模型传输到单元格?
//更新
我正在通过Saleh Altahini 帖子更新我的代码
谢谢,我尝试实现第二种方法。
我用var imageModelCell: imageModelCollectionViewCell?
及使用方法
DispatchQueue.main.async {
self.collectionView.reloadData()
imageModelCell!.modelImage = UIImage(data: data) as imageModelCollectionViewCell }
并且有错误
Cannot convert value of type 'UIImage?' to type 'imageModelCollectionViewCell' in coercion
【问题讨论】:
-
你能贴出你的单元格的代码吗?
-
它是空的。只输出label和ImageView那个有问题?我试图填写它们,但由于缺少工作方法而被删除以避免代码冲突
-
试试这个:cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! imageModelCollectionViewCell 然后 cell.image = Model.image btw swift中类的第一个字母通常是大写的
-
否,在数据源中创建单元格时,请确保使用“as!”强制向下转换
-
UIImage(data: data) as imageModelCollectionViewCell导致错误,因为您试图将 UIImage 向下转换为 imageModelCollectionViewCell。尝试删除as imageModelCollectionViewCell
标签: swift model uicollectionview cell