【问题标题】:Could not cast value of type 'UITableViewCell' (0x10f16f778) to 'ProjectName.MyCustomCell' (0x10cc5a568)无法将“UITableViewCell”(0x10f16f778)类型的值转换为“ProjectName.MyCustomCell”(0x10cc5a568)
【发布时间】:2017-09-08 13:17:43
【问题描述】:

我目前正在跟随指南 (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)')中描述的问题相同,但是其中没有一个解决方案能够解决我的问题。

这是我为解决此问题所做的各种尝试。

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

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

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

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

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

【问题讨论】:

  • self.plantList.dequeueReusableCell... vs tableView.dequeueReusableCell... 你能检查一下self.plantList 是否确实是该方法的tableView 参数吗?否则,请尝试也调用 func dequeueReusableCell(withIdentifier:, for:),(使用 IndexPath)。
  • 是的,self.plantList 是 tableView 参数。我尝试将 func 与 ', for: indexPath' 一起使用,这给了我同样的错误。
  • 情节提要中的单元格 ID 和“cellReuseIdentifier”是否相同?
  • 是的!这行代码是:let cellReuseIdentifier = "cell"
  • 故事板中可能有第二个单元原型,标识符为“单元”。三重检查。

标签: swift xcode uitableview swift3


【解决方案1】:

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

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

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

【讨论】:

  • 如果您使用情节提要,则根本不需要注册任何单元格。我想知道有多少教程建议这样做。但是,您确实在使用额外的 .xib 时需要注册单元。
【解决方案2】:

我会放一些代码,希望对你有帮助,如果你不明白,请在下面评论。

let cellReuseIdentifier = "cell"

将上面的代码行放在您的override func viewDidLoad() 上方,如下图所示

现在将下面的代码行放入你的 viewDidLoad 方法中:

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

然后,在 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 中使用以下代码行

let cell:MyCustomCellModel = yourTableviewObjc.dequeueReusableCell(withIdentifier: cellReuseIdentifier) as! MyCustomCellModel

它会起作用的。

谢谢

【讨论】:

  • 感谢您的回复!我设法通过按照我在自我回答中所说的来修复我的代码,但我相信这会帮助到这里的其他人。
  • 太棒了 :) 编码愉快,通过投票帮助这个答案传达给其他人,谢谢@Gabe
【解决方案3】:

就像您在问题中所说的那样,您在此方法中遇到错误

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
}

在这种方法中,我注意到plantList 是您在 Storyboard 中添加的 TableView 的 Outlet,并且您尝试从该表中取出一个单元格。

为什么不将MyCustomCellModel_tableView 参数对象中取出?

我认为plantList 确实包含一个MyCustomCellModel 单元格,这就是为什么它会给你一个错误。 而不是从plantList 中取出一个单元格,而是尝试从_tableView 中取出一个单元格。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多