Swift: Could not cast value of type 'UITableViewCell' to a custom cell class

I’m working on iOS app, which needs to pull the data from Firebase and display in a table view. To give it a custom design, I’ve created a custom table view cell class. When I’m trying to display the data that firebase returns in “populateCellWithBlock” method, the app crushes, displaying this error:

Could not cast value of type ‘UITableViewCell’ (0x10a73f9f0) to ‘EFRideSharing.RideTableViewCell’ (0x108117f20).

  • Pass custom class data between view controllers in Swift
  • UITableViewCell load images and reused cells
  • How to pass data to detail view after being selected in a table view?
  • if we click on Custom Section Header in UITableView, then move that section to top
  • Moving reordering control to left
  • Super.init isn't called before returning from initializer
  • dataSource = FirebaseTableViewDataSource(ref: ref, cellReuseIdentifier: "rideCell", view: self.ridesTableView)
    
     dataSource.populateCellWithBlock
            { (cell,snapshot) -> Void in
    
                let tvc: RideTableViewCell = cell as! RideTableViewCell
    
                let snapshot: FDataSnapshot = snapshot as! FDataSnapshot
    
                tvc.toLabel.text = snapshot.value["time"] as? String
    
    
        }
    

    Any ideas how to make it work?

    Update: Additional pieces of code

    class RideTableViewCell: UITableViewCell {
    
    @IBOutlet weak var timeLabel: UILabel!
    @IBOutlet weak var toLabel: UILabel!
    @IBOutlet weak var fromLabel: UILabel!
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
    
        // Configure the view for the selected state
    }
    }
    

    Solutions Collect From Internet About “Swift: Could not cast value of type 'UITableViewCell' to a custom cell class”

    Author of FirebaseUI-iOS here.

    Looks like the issue is that we’re returning a UITableViewCell rather than a RideTableViewCell due to the fact that the default class of the cell returned by FirebaseTableViewDataSource is UITableViewCell when the cellClass property isn’t set.

    You can use the version of the constructor that contains cellClass to solve this problem:

    dataSource = FirebaseTableViewDataSource(ref: ref, cellClass: RideTableViewCell.self, cellReuseIdentifier: "rideCell", view: self.ridesTableView)
    

    This will allow us to return the appropriately cast versions of the object to you. Also, to address issues of knowing the type of the cell statically on population, w can do the following.

    In Objective-C this is really easy, as we can just give the properties in the block a different type:

    [self.dataSource populateCellWithBlock:^(YourCustomCellClass *cell, FDataSnapshot *snap) {
        // Populate cell as you see fit
    }];
    

    Because we use __kindof UITableViewCell as the argument where available (id when not, so pre XCode 7), this behavior works as intended in any version of XCode, since XCode will either accept “Yes, this class is a subclass of UITableViewCell” (>= XCode 7) or “If it’s id I can still send a message to it, so I’ll allow it” (<= XCode 6.4).

    Swift is a different story. While you’d think that you should be able to do:

    dataSource.populateCellWithBlock{ (cell: YourCustomClass, snapshot: FDataSnapshot) -> Void in
        // Populate cell as you see fit
    }
    

    And get reasonable, pre-cast versions of the cells. That said, I’ve consistently gotten errors thrown saying that the method signatures don’t match comparing AnyObject to YourCustomClass and chalked it up as an Apple issue with __kindof, which is one of the least documented features I know of. If anyone has any thoughts on why this might be the case, or knows of better documentation, I’d love to hear where this is coming from (or if it’s fixed in the GM release of XCode 7).