[英]Swift: Cannot assign a value of type 'int' to a value of type 'int?' Error
[英]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!
}
我想使用上面的代码重用UITableViewCell,但是在构建它时出现了此错误。
看起来像: 无法将类型“ UITableViewCell”的值分配给类型“ ChampionThumbnailTableViewCell?”
有什么解决方案可以解决吗?
(顺便说一句,我英语不太好...)
将您的方法更改为此:
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
}
请注意,我在第三行中使用as! 而不是? 向编译器显示您对单元格为零并不感到困惑。
但是,如果事实证明您的单元格为nil, 则应导致运行时错误 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.