Abraham P
Abraham P

Reputation: 15481

Could not cast value of type 'UITableViewCell' (0x10c111c68) to subclass

I am attempting to build a fairly straight forward table of data, and am following along with this tutorial: http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift

I have the following custom cell class:

import Foundation
import UIKit

class WorkItemCell: UITableViewCell {
  @IBOutlet var titleLabel: UILabel!
}

and the following function in my TableClass:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = self.tableView.dequeueReusableCellWithIdentifier("WorkItemCell") as! WorkItemCell
    cell.textLabel?.text = self.work[indexPath.row]["name"] as! String
    return cell
}

This results in:

Could not cast value of type 'UITableViewCell' (0x10c111c68) to 'proj.WorkItemCell' (0x10a84c4c0).

Why? And how do I fix it?

Upvotes: 1

Views: 3614

Answers (2)

Abraham P
Abraham P

Reputation: 15481

Eventually, what I realized the problem was is that I was using UITableViewCell.self in my tableView.registerClass, changing that to WorkItemCell.self fixed the problem

Upvotes: 5

Imran
Imran

Reputation: 2941

Please make sure your idenrifier for the custom class is WorkItemCell and also you need to register the nib in viewdidload like

var nib = UINib(nibName: "WorkItemCell", bundle: nil)
tableView.registerNib(nib, forCellReuseIdentifier: "WorkItemCell")

and your cell code like .

var cell:WorkItemCell= self.tableView.dequeueReusableCellWithIdentifier("WorkItemCell") as! WorkItemCell

Upvotes: 1

Related Questions