0
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.

It looks like thie: Cannot assign value of type 'UITableViewCell' to type 'ChampionThumbnailTableViewCell?'

Is there any solution could fix it?

(By the way, I'm not very good at English...)

2
  • What is ChampionThumbnailTableViewCell? You should provide the code for it. Commented May 29, 2016 at 10:58
  • import UIKit class ItemThumbnailCollectionViewCell: UICollectionViewCell { @IBOutlet weak var ItemThumbnailImageView: UIImageView! @IBOutlet weak var ItemNameTextView: UITextView! } Commented May 29, 2016 at 11:11

1 Answer 1

2

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! 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.

Sign up to request clarification or add additional context in comments.

8 Comments

Thank you for your answer, I used these code before, but the reusable cell wasn't going well, I set the number of row in 129, and in iPhone6 plus there are 4 cells could see, if the reusable cell going well, I think it could only create 4 cells in order to show all of other cells. But in my case, it still create new cells until 129. sorry for my bad english...
Hey, i couldnt understand you properly. But, if you're saying that you set number of rows to 129, then the table will create 129 cell no matter what. I think you meant that your phone onlu shows 4 cells at a time, but that wouldn't make other 125 cells go lost. because you may wanna scroll... i am sorry if i didnt get you
Ok, maybe I should practice english before asking question, anyway thank you very much, have a nice day!
I'm sorry I couldn't help. But, your above comment was written in perfect English, just saying :) Good day!
oh, it is my pleasure to hear that, if you don't mind can i ask you one more question about collectionviewcell by facebook or some other chat app please?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.