xcode 无法将“UITableViewCell”类型的值转换为“AppName.CustomCellName”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33436352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Could not cast value of type 'UITableViewCell' to 'AppName.CustomCellName'
提问by Async-
I am trying to create a custom cell that expands on tap. I am using this github example: https://github.com/rcdilorenzo/Cell-Expander
我正在尝试创建一个可随时扩展的自定义单元格。我正在使用这个 github 示例:https: //github.com/rcdilorenzo/Cell-Expander
This line gives runtime error SIGABRT:
这一行给出了运行时错误 SIGABRT:
override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
(cell as! EventTableViewCell).watchFrameChanges()
//Could not cast value of type 'UITableViewCell' (0x105aa1b80) to 'AppName.EventTableViewCell' (0x104287fe0).
}
I also checked answer from this post, followed three steps, but no luck: Could not cast value of type 'UITableViewCell' to '(AppName).(CustomCellName)'
我还检查了这篇文章的答案,遵循了三个步骤,但没有运气: Could not cast value of type 'UITableViewCell' to '(AppName).(CustomCellName)'
My custom cell class looks like this:
我的自定义单元格类如下所示:
import UIKit
class EventTableViewCell : UITableViewCell {
...
}
cellForRowAtIndexPath:
cellForRowAtIndexPath:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier( "eventCell", forIndexPath: indexPath)
let date = self.eventArray[indexPath.row].startTime
let calendar = NSCalendar.currentCalendar()
let minutes = calendar.component(NSCalendarUnit.Minute, fromDate: date)
var minutesString: String
if (minutes == 0) {
minutesString = "00"
} else {
minutesString = String(calendar.component(NSCalendarUnit.Minute, fromDate: date))
}
let hours = calendar.component(NSCalendarUnit.Hour, fromDate: date)
cell.textLabel?.text = self.eventArray[indexPath.row].title + " - \(hours):\(minutesString)"
return cell
}
}
Please help.
请帮忙。
回答by Greg Brown
"I have self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "eventCell") in my viewDidLoad"
“我的 viewDidLoad 中有 self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "eventCell")”
That's probably overriding the value you set in the storyboard. Try removing this line, or change it to self.tableView.registerClass(EventTableViewCell.self, forCellReuseIdentifier: "eventCell")
.
这可能会覆盖您在故事板中设置的值。尝试删除此行,或将其更改为self.tableView.registerClass(EventTableViewCell.self, forCellReuseIdentifier: "eventCell")
.
回答by Async-
As @Greg Brown said, solution was to change the type of the cell to custom cell class:
正如@Greg Brown 所说,解决方案是将单元格的类型更改为自定义单元格类:
//wrong:
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "eventCell")
//right:
self.tableView.registerClass(EventTableViewCell.self, forCellReuseIdentifier: "eventCell")
回答by MobileMon
In the storyboard, click on the cell and set the class name to EventTableViewCell instead of UITableViewCell
在storyboard中,点击单元格并将类名设置为EventTableViewCell而不是UITableViewCell
Or if you are doing everything programmatically, in cellForRowAtIndexPath do this instead:
或者,如果您以编程方式执行所有操作,请在 cellForRowAtIndexPath 中执行以下操作:
var cell : EventTableViewCell?
cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as! EventTableViewCell?
if cell == nil {
cell = EventTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "eventCell")
}
return cell
回答by Greg Brown
Are you sure the cell you're getting is an instance of EventTableViewCell
? Did you register your custom cell class with registerClass:forCellReuseIdentifier:
?
你确定你得到的细胞是一个实例EventTableViewCell
吗?您是否使用 注册了自定义单元格类registerClass:forCellReuseIdentifier:
?
If you change your code from this:
如果您从此更改代码:
(cell as! EventTableViewCell).watchFrameChanges()
to this:
对此:
(cell as? EventTableViewCell)?.watchFrameChanges()
do you still get the exception?
你仍然得到例外吗?
Also, are you using the same storyboard from the GitHub example? If so, did you update the storyboard to use your cell class name and reuse identifier? You are using EventTableViewCell/"eventCell" but the original code used PickerTableViewCell/"cell".
另外,您使用的是 GitHub 示例中的同一个故事板吗?如果是这样,您是否更新故事板以使用您的单元格类名称并重用标识符?您正在使用 EventTableViewCell/"eventCell" 但原始代码使用 PickerTableViewCell/"cell"。