Failed to pass value of type "UITableViewCell" to "AppName.CustomCellName"

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

This line displays the SIGABRT runtime error:

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 the answer to this post, followed three steps, but no luck: Unable to pass a value of type 'UITableViewCell' to '(AppName). (CustomCellName) '

My own cell class is as follows:

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.

+4
source share
4 answers

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

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

+14

EventTableViewCell UITableViewCell

, , cellForRowAtIndexPath :

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

, , , EventTableViewCell? registerClass:forCellReuseIdentifier:?

:

(cell as! EventTableViewCell).watchFrameChanges()

:

(cell as? EventTableViewCell)?.watchFrameChanges()

?

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

+1
source

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

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

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

Source: https://habr.com/ru/post/1613836/


All Articles