我目前正在尝试使用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
}

感谢您的帮助。我似乎无法使它正常工作。

最佳答案

在这种情况下,您可以检查以下几件事:

  • 通常通过@IBOutlet weak var tableView: UITableView!来查看表是否链接到您的类
  • 注册自定义表格 View 单元格:self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")请注意,即使您的自定义单元格具有不同的类和ID,也要使用“UITableViewCell”和标识符“cell”。
  • 使您的单元格在cellForRowAtIndexPath中出队:let cell: MessageCell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell现在,您使用正确的单元格标识符。
  • 08-05 22:16
    查看更多