我有一个保存布尔值的模型。我想使用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_hold的评论,使用细胞标识符作为String(describing: Self)
时出现问题,因为我曾一度重命名过该类,这导致标识符中断。
我将标识符更改为原始值字符串,这样就解决了问题。