6

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.

Community
  • 1
  • 1
Async-
  • 3,140
  • 4
  • 27
  • 49
  • show your code for method cellForRowAtIndexPath – MobileMon Oct 30 '15 at 12:54
  • @MobileMon edited the question^ – Async- Oct 30 '15 at 13:00
  • Possible duplicate of [Could not cast value of type 'UITableViewCell' to '(AppName).(CustomCellName)'](https://stackoverflow.com/questions/29812168/could-not-cast-value-of-type-uitableviewcell-to-appname-customcellname) – Naresh May 18 '18 at 09:14

4 Answers4

20

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

Greg Brown
  • 3,168
  • 1
  • 27
  • 37
  • I was making my Table View programmatically and not setting the custom cell name in the `registerClass` method was what caused the error for me. – Suragch Apr 09 '16 at 06:57
2

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
MobileMon
  • 8,341
  • 5
  • 56
  • 75
1

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

Greg Brown
  • 3,168
  • 1
  • 27
  • 37
  • I have self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "eventCell") in my viewDidLoad, and eventCell is the identifier from the storyboard. It indeed has to do something with registering cell, because your code works, but when I try to drag cell (other function), I get same error: Could not cast value of type 'UITableViewCell' (0x10d29eb80) to 'CallIn.EventTableViewCell – Async- Oct 30 '15 at 13:46
  • I am not using same storyboard, I use mine, which I adapted to fit. – Async- Oct 30 '15 at 13:47
  • If you have defined "eventCell" in the storyboard, I don't think you need to explicitly register it in code. What type did you set for the cell in the storyboard? Is it EventTableViewCell? – Greg Brown Oct 30 '15 at 13:49
  • if you are not using storyboard are you using a xib? – MobileMon Oct 30 '15 at 13:49
  • @MobileMon - I think he means that he's not using the same storyboard from the GitHub project, he is using his own storyboard. – Greg Brown Oct 30 '15 at 13:50
  • yes, I use my storyboard, and I changed it. Yes, I have class of the cell in storyboard: EventTableViewCell and identifier: eventCell.. – Async- Oct 30 '15 at 13:53
  • when I hit the cell, my method didSelectRowAtIndexPath does not fire. Why? – Async- Oct 30 '15 at 13:57
  • If you set a breakpoint in tableView:cellForRowAtIndexPath:, what type is your cell instance? – Greg Brown Oct 30 '15 at 14:02
  • 1
    "I have self.tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "eventCell")" - try removing this line, or change it to `self.tableView.registerClass(EventTableViewCell.self, forCellReuseIdentifier: "eventCell")`. – Greg Brown Oct 30 '15 at 14:11
  • this worked for now :) thank you, can you put this as an answer, I will accept it? – Async- Oct 30 '15 at 14:19
  • Glad to help. Answer added. :-) – Greg Brown Oct 30 '15 at 14:22
1

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")
Async-
  • 3,140
  • 4
  • 27
  • 49