根据 Switch 语句中的模型数据返回单元格

问题描述

我有一个保存布尔值的模型。我想使用 switch 语句根据模型中 bool 的值返回 UICollectionViewCell 。但是,我遇到了转换错误。

Could not cast value of type 'Project.FalseCell' (0x10077a9f0) to 'Project.TrueCell' (0x100777ef8).

我已经检查了以下内容;

  1. 我已在 viewDidLoad
  2. 中注册了我的单元格
  3. 我对每种细胞类型都有唯一的标识符

我在这里遗漏了什么?

// MODEL
struct Model {
    let bool: Bool
}

// CONTROLLER
let models = [Model]()

func collectionView(_ collectionView: UICollectionView,cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    let model = models[indexPath.item]
    
    switch model.bool {
    case true:
        let trueCell: TrueCell = listView.collectionView.dequeueReusableCell(withReuseIdentifier: TrueCell.identifier,for: indexPath) as! TrueCell
        return trueCell
    case false:
        let falseCell: FalseCell = listView.collectionView.dequeueReusableCell(withReuseIdentifier: FalseCell.identifier,for: indexPath) as! FalseCell
        return falseCell
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)