![](/img/trans.png)
[英]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.