obo20
obo20

Reputation: 471

Could not cast value of type 'UITableViewCell' to '(AppName).(CustomCellName)'

I'm currently trying to create a custom table view cell using xCode 6.3 swift 1.2. For some reason in the cellforRowAtIndexPath method, I just can't seem to set up my cell variable. The code will compile, but then when this line of code hits:

    var cell:MessageCell = tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell

I get this Error: Could not cast value of type 'UITableViewCell' (0x1112f5a18) to 'CampusExchange.MessageCell' (0x10e8318f0).

Here's my full method: (I'm using Parse if you're wondering about how I'm setting the message)

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    var cell:MessageCell = tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell

    let message = messagesArray[indexPath.row]["Message"] as? String
    cell.messageOutlet.text = message
    return cell
}

thanks for any help you might have. I just can't seem to get this to work.

Upvotes: 41

Views: 49904

Answers (12)

M A Russel
M A Russel

Reputation: 1557

Keep the custom cell class name and cell id same:

   self.tableView.register(CustomCell.self, forCellReuseIdentifier: "CustomCell")

Upvotes: 7

Naresh
Naresh

Reputation: 17892

In my case i'm not changed the UITableViewCell class. Initially it's DetailsTableViewCell class but i removed and added new class called TableViewCellClass. But I forget to change the cell class name here.

So once again check and change TableViewCell class name here see below screen shot

enter image description here

Upvotes: 5

Patrik Rikama-Hinnenberg
Patrik Rikama-Hinnenberg

Reputation: 1600

X Code 9.4
Unlike one of the popular answer here this solution fixed my nightmare with custom tableview cells. If you are using storyboard and everything is hooked up correctly then you do not have to register the the custom tableView cell in viewDidLoad.

Remove from viewDidLoad:
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

Upvotes: 11

Rodrigo Pinto
Rodrigo Pinto

Reputation: 2414

There are a few things you can check in this scenario:

  1. See if your table is linked to your class, usually by @IBOutlet weak var tableView: UITableView!

  2. Register custom table view cell: self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell") Note that you use "UITableViewCell" and the identifier "cell" even if your custom cell has different class and id.

  3. Dequeue your cell in cellForRowAtIndexPath: let cell: MessageCell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell Now you use the correct cell identifier.

Upvotes: 77

Deepti Raghav
Deepti Raghav

Reputation: 902

I had same problem , I set custom class for cell but forgot to set module enter image description here

after selecting module like this , it worked

enter image description here

Upvotes: 19

daniel pham
daniel pham

Reputation: 1

Recreate your class using cocoaClass and select UItableViewCell

I had the same problem this worked for me.

Upvotes: 0

Jacksonsox
Jacksonsox

Reputation: 1233

I had the same error. As obo2O states in his comment, his solution worked for me:

  1. Go to the Interface Builder
  2. Click on the relevant storyboard
  3. Click on the relevant cell within the tableview
  4. In the Utilities Panel (right side), click on the "Show the Identify Inspector" icon
  5. In the "Custom Class" section -> "Class" field, set the class to anything else and run the application. The application crashes, for classX cannot cast to classY
  6. In the "Custom Class" section -> "Class" field, set the class to the correct class and re-run.

Upvotes: 0

bj97301
bj97301

Reputation: 341

Swift 3 syntax:

self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")

Upvotes: 0

Madhurya Gandi
Madhurya Gandi

Reputation: 892

Register your CustomCell in viewDidLoad() method:

self.tableView.register(CustomCell.self, forCellReuseIdentifier: "Cell")

Upvotes: 48

Milander
Milander

Reputation: 1061

I've had the same error message. But for me the problem was that I didn't set the class of my custom-cell in the interface builder.

Upvotes: 5

Daniel Chepenko
Daniel Chepenko

Reputation: 2268

Firstly I suggest you to recreate your cell class, using CocoaClass. I had quite similar mistake - I created a CollectionViewCell, and when I recognised my mistake, I decided to simply rename the parent class. However, compiler haven't notice any mistake, I had this error during build.

Upvotes: 0

Long Pham
Long Pham

Reputation: 7582

In viewDidLoad()

// register custom table view cell from nib
self.tableView.registerNib(UINib(nibName: "MessageCell", bundle: nil), forCellReuseIdentifier: "messageCell")

Upvotes: 9

Related Questions