ios - 在 Switch 语句中根据模型数据返回单元格
问题描述
我有一个持有布尔值的模型。我想使用 switch 语句根据模型中 bool 的值返回 UICollectionViewCell 。但是,我遇到了一个铸造错误。
Could not cast value of type 'Project.FalseCell' (0x10077a9f0) to 'Project.TrueCell' (0x100777ef8).
我检查了以下内容;
- 我已经注册了我的细胞
viewDidLoad
- 我对每种细胞类型都有唯一的标识符
我在这里想念什么?
// 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
}
}
解决方案
根据@goat_herd 的评论,使用单元标识符存在问题,String(describing: Self)
因为我曾一度重命名该类,这导致标识符中断。
我将标识符更改为原始值字符串,这解决了这个问题。
推荐阅读
- python - 使用 TensorFlow 的对象检测 API
- javascript - 如何构建对 API 的异步调用?
- printing - 如何在python3中创建打印对话框
- swift - tableView 内的 collectionView 在滚动时返回到第一个索引
- javascript - 将构造函数中的默认值设置为未定义是一种好习惯吗?
- python - 如何通过标记现有数据框的内容来创建新的数据框?
- reactjs - Highcharts 系列标签
- angular - 当表单控件验证器更改以避免 expressionchangedafterithasbeencheckederror 时,为什么我必须调用 this.cdRef.detectChanges()?
- latex - 使用 tlmgr 搜索所有关键字
- javascript - 浏览器选项卡的活动如何影响查询