简体   繁体   English

swift无法将类型“ UITableViewCell”的值分配为类型“…Cell”的错误

[英]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.

相关问题 Swift:无法为“ int”类型的值分配“ int”类型的值? 错误 - Swift: Cannot assign a value of type 'int' to a value of type 'int?' Error Swift3错误:无法将类型'()'的值分配给类型[[customObject]? - Swift3 error: Cannot assign value of type '()' to type '[customObject]?' 错误:无法将类型“[String]”的值分配给 swift 中的“String”类型 - Error: Cannot assign value of type '[String]' to type 'String' in swift Swift 3 Generic无法转换UITableViewCell类型的值 - Swift 3 Generic cannot convert value of type UITableViewCell Swift不能指定类型'()'的值来键入'String?' - Swift cannot assign value of type '()' to type 'String?' prepareForSegue方法中出现错误“无法分配类型的值”(Swift,Xcode 6) - Error “cannot assign a value of type” in prepareForSegue method (Swift, Xcode 6) Swift:无法指定'Item'类型的值?输入'Item?.Type' - Swift: Cannot assign value of type 'Item?' to type 'Item?.Type' 无法将“Int”类型的值分配给“Int?.Type”类型 Swift 5 - Cannot assign value of type 'Int' to type 'Int?.Type' Swift 5 SWIFT:无法分配类型…/ SwiftyJSON的值 - SWIFT : Cannot assign value of type … / SwiftyJSON Swift无法分配'UINavigationController'类型的值 - Swift cannot assign value of type 'UINavigationController'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM