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"的值分配给类型"冠军缩略图表视图单元格?
有什么解决方案可以解决它吗?
(顺便说一句,我的英语不是很好...
将方法更改为:
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!而不是as? 向编译器显示您不会对单元格为零感到困惑。
但是,如果事实证明您的单元格为零,则应该会导致运行时错误。