Search code examples
iosiphoneswiftuitableviewios9

Issues with Deleting rows when using a custom class


I am currently trying to code a table row deletion (from Core Data). I am using a custom class called "CustomTableViewCell". Everything was working OK until I implemented a snippet from a previous app which basically just add the swipe to delete feature. I am now getting the following error:

Cannot convert value of type 'UITableViewCell' to expected argument type 'CustomTableViewCell'

The offending code is as it follows:


func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    switch type {
    case .Insert:
        tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
    case .Delete:
        tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
    case .Update:
        self.configureCell(tableView.cellForRowAtIndexPath(indexPath!)!, withObject: anObject as! NSManagedObject) // THIS IS THE OFFENDING CASE
    case .Move:
        tableView.moveRowAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
    }
}

The method is pretty simple: I believe it's the one used in the Master Detail template. The difference is the custom class.

Any ideas?


Solution

  • It looks like tableView.cellForRowAtIndexPath(indexPath!) is returning a UITableViewCell. You probably need to cast that into your custom type with as! CustomTableViewCell before you pass it to your method.