41

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.

3
  • 10
    It looks like you haven't set the custom class for your prototype cell in the storyboard
    – Paulw11
    Apr 23, 2015 at 1:54
  • 4
    I had it set. But for some reason it wasn't working. What randomly worked was deleting the custom class for my cell. Then building, crashing the application, and then going back and resetting the custom class. Now it appears to work. Thanks for pointing me in the right direction though!
    – obo20
    Apr 23, 2015 at 1:59
  • Same here, I had Custom Class set, but it still gave me that error. I just clicked on the field and hit enter, to sort of nudge it, and after that it worked. Xcode bug probably?
    – Emil
    Mar 12, 2021 at 4:57

12 Answers 12

77

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.

6
  • 13
    In my particular case the code was correct but in the storyboard where I created the UITableviewCell I forgot to set my custom class with the inspector for the UITableViewCell. In the inspector under Custom Class it still said "UITableViewCell" when it should have said "UserInfoCell" which was the name of the custom subclass I had created.
    – xdeleon
    Jan 3, 2016 at 4:12
  • Point 2. help a lot of. Thanks! May 19, 2016 at 7:00
  • 2
    Point 2 was the problem for me as well! This is crucial since with a normal tableview controller the cell to be registered isn't specified so it's fairly intuitive to assume that the reuse identifier should be used for both place. Took me forever to find this answer. Thanks alot Mar 3, 2017 at 10:29
  • 1
    Adding myself to say Point #2 was super helpful. Thanks so much!
    – Lucy
    Nov 18, 2017 at 14:05
  • you don't need to do all these steps programmatically, you have storyboard, check developer.apple.com/library/archive/referencelibrary/…
    – user25
    Mar 23, 2019 at 19:55
48

Register your CustomCell in viewDidLoad() method:

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

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

1
11

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")

1
  • this is what did it for me
    – mondousage
    Aug 10, 2022 at 22:16
9

In viewDidLoad()

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

Keep the custom cell class name and cell id same:

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

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.

5

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

0
0

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.

0

Swift 3 syntax:

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

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

Recreate your class using cocoaClass and select UItableViewCell

I had the same problem this worked for me.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.