ios - 无法将类型 'UITableViewCell' 的值转换为 '(AppName).(CustomCellName)'

标签 ios swift xcode uitableview cocoa-touch

我目前正在尝试使用 xCode 6.3 swift 1.2 创建自定义表格 View 单元格。由于某些原因,在 cellforRowAtIndexPath 方法中,我似乎无法设置我的单元格变量。代码将编译,但是当这行代码命中时:

    var cell:MessageCell = tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell

我收到此错误:无法将类型“UITableViewCell”(0x1112f5a18)的值转换为“CampusExchange.MessageCell”(0x10e8318f0)。

这是我的完整方法:(如果您想知道我是如何设置消息的,我正在使用 Parse)

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:MessageCell = tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell

    let message = messagesArray[indexPath.row]["Message"] as? String
    cell.messageOutlet.text = message
    return cell
}

感谢您提供的任何帮助。我似乎无法让它发挥作用。

最佳答案

在这种情况下,您可以检查一些事项:

  1. 查看你的表是否链接到你的类,通常是通过@IBOutlet weak var tableView: UITableView!

  2. 注册自定义表格 View 单元格:self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") 请注意,您使用“UITableViewCell”和标识符“cell”,即使您的自定义单元格具有不同的类和 ID。

  3. 在 cellForRowAtIndexPath 中使您的单元格出队: let cell: MessageCell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as!消息单元格 现在您使用正确的单元格标识符。

关于ios - 无法将类型 'UITableViewCell' 的值转换为 '(AppName).(CustomCellName)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812168/

相关文章:

ios - 使用 Swift 的 Alamofire - "Could not cast value of type ' Swift._SwiftDeferredNSArray' (ox10a75ebb0) 到 'Photomania.PhotoInfo' (0x107ee7b90)。”

ios - 忽略状态栏的自动布局

ios - UIAutomation 不适用于 IPA 的分发类型

ios - 获取 NSGenericException 原因为 : '*** Collection <NSConcreteHashTable: 0x282c34140> was mutated while being enumerated.'

ios - 从桌面或服务器以编程方式访问(您自己的)iCloud 数据?

ios - Swift/iOS : using computed property to trigger UI update, : didSet, will set 和 set 哪个更好?

ios - 在ARSCNView中获取CGPoint的世界坐标(Swift)

ios - 如何在 Swift 中传递 Realm 结果?

ios - 无法降低 ios 中的动画速度

ios - 在 ContactsUI 中显示联系人的 EditView