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.
There are a few things you can check in this scenario:
See if your table is linked to your class, usually by @IBOutlet weak var tableView: UITableView!
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.
Dequeue your cell in cellForRowAtIndexPath: let cell: MessageCell = self.tableView.dequeueReusableCellWithIdentifier("messageCell") as! MessageCell
Now you use the correct cell identifier.
Register your CustomCell in viewDidLoad()
method:
self.tableView.register(CustomCell.self, forCellReuseIdentifier: "Cell")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With