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

前端 未结 12 1669
一生所求
一生所求 2020-12-02 23:03

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

相关标签:
12条回答
  • 2020-12-02 23:14

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

    0 讨论(0)
  • 2020-12-02 23:19

    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 讨论(0)
  • 2020-12-02 23:27

    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.

    0 讨论(0)
  • 2020-12-02 23:27

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

    after selecting module like this , it worked

    0 讨论(0)
  • 2020-12-02 23:29

    Recreate your class using cocoaClass and select UItableViewCell

    I had the same problem this worked for me.

    0 讨论(0)
  • 2020-12-02 23:32

    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.

    0 讨论(0)
提交回复
热议问题