[英]Cannot assign value of type 'UITableViewCell' to type '…Cell' error in swift
let cellIdentifier = "ChampionThumbnailCell"
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as? ChampionThumbnailTableViewCell
if cell == nil {
cell = UITableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: cellIdentifier)
}
cell!.championNameLabel.text = championNames[indexPath.row]
cell!.championThumbnailImageView.image = UIImage(named: championThumbnail[indexPath.row])
return cell!
}
I want to use the above code to reuse UITableViewCell, but I got this error when build it. 我想使用上面的代码重用UITableViewCell,但是在构建它时出现了此错误。
It looks like thie: Cannot assign value of type 'UITableViewCell' to type 'ChampionThumbnailTableViewCell?' 看起来像: 无法将类型“ UITableViewCell”的值分配给类型“ ChampionThumbnailTableViewCell?”
Is there any solution could fix it? 有什么解决方案可以解决吗?
(By the way, I'm not very good at English...) (顺便说一句,我英语不太好...)
Change your method to this: 将您的方法更改为此:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cellIdentifier = "ChampionThumbnailCell"
var cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ChampionThumbnailTableViewCell
cell.championNameLabel.text = championNames[indexPath.row]
cell.championThumbnailImageView.image = UIImage(named: championThumbnail[indexPath.row])
return cell
}
Notice in third line I am using as! 请注意,我在第三行中使用as! instead of as? 而不是? showing compiler that you aren't confused about your cell being nil. 向编译器显示您对单元格为零并不感到困惑。
But if it turns out that your cell is nil, it should cause runtime error . 但是,如果事实证明您的单元格为nil, 则应导致运行时错误 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.