Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 958
obo20 Avatar asked Apr 23 '15 01:04

obo20


2 Answers

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.

like image 72
Rodrigo Pinto Avatar answered Oct 11 '22 14:10

Rodrigo Pinto


Register your CustomCell in viewDidLoad() method:

self.tableView.register(CustomCell.self, forCellReuseIdentifier: "Cell") 
like image 25
Madhurya Gandi Avatar answered Oct 11 '22 12:10

Madhurya Gandi