【问题标题】:passing data from model to collectionViewCell将数据从模型传递到 collectionViewCell
【发布时间】: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


【解决方案1】:

您收到的错误意味着您的单元格未向下转换为imageModelCollectionViewCell。也许您没有正确引用单元格?

无论如何,您可以通过两种方式设置单元格。 第一种方法是在cellForItemAt 函数中设置您的单元格,如下所示:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! imageModelCollectionViewCell
    cell.modelImage.image = //Your Image
    return cell
}

或者您可以在开始时引用您的单元格,然后在其他任何地方进行设置。只需将var imageModelCell: imageModelCollectionViewCell 之类的变量添加到 UICollectionViewDataSource 并像这样传递cellForItemAt 中的单元格:

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "imageCell", for: indexPath) as! imageModelCollectionViewCell
    self.imageModelCell = cell
    return cell
}

然后您可以在任何其他函数或回调中使用imageModelCell.modelImage = //Your Image

附注:最好以大写字母开头类名和以小写字母开头的变量,这样您可以更好地区分使用 Xcode 调用或引用的内容。也许考虑将你的类的名称更改为 ImageModelCollectionViewCell

【讨论】:

  • 那是因为你把它放在了一个扩展中。如果您只是使用扩展来管理 CollectionView,则需要将其从扩展更改为类实例,并将 var 放在类的开头,例如 class NameOfClass : ViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout{ var imageModelCell: imageModelCollectionViewCell
  • 我在//update标记下的帖子中更新信息你可以看看吗?
猜你喜欢
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多