无法将“UITableViewCell"类型的值转换为“(AppName).(CustomCellName)" [英] Could not cast value of type 'UITableViewCell' to '(AppName).(CustomCellName)'
问题描述
我目前正在尝试使用 xCode 6.3 swift 1.2 创建自定义表格视图单元格.出于某种原因,在 cellforRowAtIndexPath 方法中,我似乎无法设置我的单元格变量.代码会编译,但是当这行代码命中时:
I'm currently trying to create a custom table view cell using xCode 6.3 swift 1.2. For some reason in the cellforRowAtIndexPath method, I just can't seem to set up my cell variable. The code will compile, but then when this line of code hits:
var cell:MessageCell = tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell
我收到此错误:无法将UITableViewCell"(0x1112f5a18) 类型的值转换为CampusExchange.MessageCell"(0x10e8318f0).
I get this Error: Could not cast value of type 'UITableViewCell' (0x1112f5a18) to 'CampusExchange.MessageCell' (0x10e8318f0).
这是我的完整方法:(如果您想知道我如何设置消息,我正在使用 Parse)
Here's my full method: (I'm using Parse if you're wondering about how I'm setting the message)
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
}
感谢您提供的任何帮助.我似乎无法让它发挥作用.
thanks for any help you might have. I just can't seem to get this to work.
推荐答案
在这种情况下,您可以检查以下几点:
There are a few things you can check in this scenario:
查看您的表是否链接到您的类,通常通过
@IBOutlet weak var tableView: UITableView!
注册自定义表格视图单元格:self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
请注意,即使您的自定义单元格具有不同的类和 ID,您也要使用UITableViewCell"和标识符单元格".
Register custom table view cell: self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
Note that you use "UITableViewCell" and the identifier "cell" even if your custom cell has different class and id.
在 cellForRowAtIndexPath 中出列您的单元格:让单元格:MessageCell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as!MessageCell
现在您使用了正确的单元格标识符.
Dequeue your cell in cellForRowAtIndexPath: let cell: MessageCell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell
Now you use the correct cell identifier.
这篇关于无法将“UITableViewCell"类型的值转换为“(AppName).(CustomCellName)"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!