我目前正在尝试使用 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")
请注意,您使用“UITableViewCell”和标识符“cell”,即使您的自定义单元格具有不同的类和 ID。在 cellForRowAtIndexPath 中使您的单元格出队:
let cell: MessageCell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as!消息单元格
现在您使用正确的单元格标识符。
关于ios - 无法将类型 'UITableViewCell' 的值转换为 '(AppName).(CustomCellName)',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29812168/