无法将'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:单元格)
请注意,即使您的自定义单元格具有不同的类和ID,也使用UITableViewCell和标识符cell。
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中将您的单元格取消:让cell:MessageCell = self.tableView.dequeueReusableCellWithIdentifier(messageCell)为! 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屋!