gpt4 book ai didi

swift - 无法将类型 'UITableViewCell' (0x10f16f778) 的值转换为 'ProjectName.MyCustomCell' (0x10cc5a568)

转载 作者:行者123 更新时间:2023-11-28 08:12:26 24 4
gpt4 key购买 nike

我目前正在按照指南 ( creating custom tableview cells in swift ) 在 Swift 中为 UITableView 创建自定义单元格。但是,我在运行项目时遇到错误。下面是触发错误的代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

// ***** Error triggers on the line below ******
let cell:MyCustomCellModel = self.plantList.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCellModel

cell.plantImage.backgroundColor = self.colors[indexPath.row]
cell.plantLabel.text = self.animals[indexPath.row]

return cell
}

我的问题与其他 stackoverflow 帖子(即:Could not cast value of type 'UITableViewCell' to '(AppName).(CustomCellName)')中描述的问题相同,但是其中没有一个解决方案能够解决我的问题。

以下是我为解决该问题所做的各种尝试。

  • 检查单元格是否具有正确的类和标识符。我首先检查了这个,下面是两个相关的屏幕截图。 Class Name Identifier

  • 我已经尝试了以下两种代码解决方案来注册我的类(我将其放入 viewdidload 方法),但都没有提供修复:

    self.plantList.register(MyCustomCellModel.self, forCellReuseIdentifier: "cell")self.plantList.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

  • 我还尝试了许多小修复,这些修复对在有关此问题的各种 stackoverflow 帖子中提到的人有效,但没有一个对我有用。

如有任何提示,我们将不胜感激。如果需要任何其他源代码来调试它,我会很乐意发布它。

最佳答案

好吧,我有点傻,但将此作为答案发布,以防它确实对任何人有帮助。我在viewdidload函数中不小心留下了下面这行,导致它基本上被忽略了。无论如何,感谢那些试图提供帮助的人!

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellReuseIdentifier)

^ 该行来自 UITableView 教程的简单版本,可以通过我在问题中发布的链接找到。当我试图使我的表更复杂时,它意外地留在了 viewdidload 函数的末尾。

关于swift - 无法将类型 'UITableViewCell' (0x10f16f778) 的值转换为 'ProjectName.MyCustomCell' (0x10cc5a568),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43374952/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com