Async-
Async-

Reputation: 3289

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

I am trying to create a custom cell that expands on tap. I am using this github example: https://github.com/rcdilorenzo/Cell-Expander

This line gives runtime error SIGABRT:

override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
    (cell as! EventTableViewCell).watchFrameChanges()
    //Could not cast value of type 'UITableViewCell' (0x105aa1b80) to 'AppName.EventTableViewCell' (0x104287fe0).
}

I also checked answer from this post, followed three steps, but no luck: Could not cast value of type 'UITableViewCell' to '(AppName).(CustomCellName)'

My custom cell class looks like this:

import UIKit

class EventTableViewCell : UITableViewCell {
...
}

cellForRowAtIndexPath:

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier( "eventCell", forIndexPath: indexPath)

    let date = self.eventArray[indexPath.row].startTime
    let calendar = NSCalendar.currentCalendar()
    let minutes = calendar.component(NSCalendarUnit.Minute, fromDate: date)
    var minutesString: String
    if (minutes == 0) {
        minutesString = "00"
    } else {
        minutesString = String(calendar.component(NSCalendarUnit.Minute, fromDate: date))
    }
    let hours = calendar.component(NSCalendarUnit.Hour, fromDate: date)
    cell.textLabel?.text = self.eventArray[indexPath.row].title + " - \(hours):\(minutesString)"

    return cell
    }
}

Please help.

Upvotes: 6

Views: 11807

Answers (4)

Async-
Async-

Reputation: 3289

As @Greg Brown said, solution was to change the type of the cell to custom cell class:

//wrong:
self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "eventCell") 

//right:
self.tableView.registerClass(EventTableViewCell.self, forCellReuseIdentifier: "eventCell")

Upvotes: 1

Greg Brown
Greg Brown

Reputation: 3254

"I have self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "eventCell") in my viewDidLoad"

That's probably overriding the value you set in the storyboard. Try removing this line, or change it to self.tableView.registerClass(EventTableViewCell.self, forCellReuseIdentifier: "eventCell").

Upvotes: 20

MobileMon
MobileMon

Reputation: 8651

In the storyboard, click on the cell and set the class name to EventTableViewCell instead of UITableViewCell

Or if you are doing everything programmatically, in cellForRowAtIndexPath do this instead:

 var cell : EventTableViewCell?
 cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) as! EventTableViewCell?
 if cell == nil {
           cell = EventTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: "eventCell")
 }
return cell

Upvotes: 2

Greg Brown
Greg Brown

Reputation: 3254

Are you sure the cell you're getting is an instance of EventTableViewCell? Did you register your custom cell class with registerClass:forCellReuseIdentifier:?

If you change your code from this:

(cell as! EventTableViewCell).watchFrameChanges()

to this:

(cell as? EventTableViewCell)?.watchFrameChanges()

do you still get the exception?

Also, are you using the same storyboard from the GitHub example? If so, did you update the storyboard to use your cell class name and reuse identifier? You are using EventTableViewCell/"eventCell" but the original code used PickerTableViewCell/"cell".

Upvotes: 1

Related Questions