无法将类型'UITableViewCell'(0x10c111c68)的值转换为子类

时间:2015-10-02 02:48:06

标签: ios swift uitableview

我正在尝试构建一个相当直接的数据表,并遵循本教程: http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

我有以下自定义单元格类:

import Foundation
import UIKit

class WorkItemCell: UITableViewCell {
  @IBOutlet var titleLabel: UILabel!
}

以及我的TableClass中的以下函数:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("WorkItemCell") as! WorkItemCell
    cell.textLabel?.text = self.work[indexPath.row]["name"] as! String
    return cell
}

这导致:

Could not cast value of type 'UITableViewCell' (0x10c111c68) to 'proj.WorkItemCell' (0x10a84c4c0).

为什么呢?我该如何解决?

2 个答案:

答案 0 :(得分:4)

最终,我意识到问题在于我在UITableViewCell.self中使用tableView.registerClass,将其更改为WorkItemCell.self解决了问题

答案 1 :(得分:1)

请确保您的自定义类的标识符是WorkItemCell,还需要在viewdidload中注册nib,如

var nib = UINib(nibName: "WorkItemCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "WorkItemCell")

和你的单元格代码一样。

var cell:WorkItemCell= self.tableView.dequeueReusableCellWithIdentifier("WorkItemCell") as! WorkItemCell
相关问题