gpt4 book ai didi

ios - 无法将类型 'UITableViewCell' 的值分配给 swift 中的类型 '...Cell' 错误

转载 作者:搜寻专家 更新时间:2023-11-01 07:23:29 25 4
gpt4 key购买 nike

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,但是在build的时候出现了这个错误。

看起来像这样:无法将类型“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,应该会导致运行时错误

关于ios - 无法将类型 'UITableViewCell' 的值分配给 swift 中的类型 '...Cell' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37508993/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com