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

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

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.

回答1:

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.



回答2:

Register your CustomCell in viewDidLoad() method:

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


回答3:

In viewDidLoad()

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


回答4:

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:

I had same problem , I set custom class for cell but forgot to set module

after selecting module like this , it worked



回答6:

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.



回答7:

Swift 3 syntax:

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


回答8:

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.


回答9:

Recreate your class using cocoaClass and select UItableViewCell

I had the same problem this worked for me.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!