使用自定义 class 时删除行的问题
Issues with Deleting rows when using a custom class
我目前正在尝试编写 table 行删除(来自 Core Data)。我正在使用名为 "CustomTableViewCell" 的自定义 class。一切正常,直到我从以前的应用程序中实现了一个片段,该片段基本上只是添加了滑动删除功能。我现在收到以下错误:
无法将类型 'UITableViewCell' 的值转换为预期的参数类型 'CustomTableViewCell'
违规代码如下:
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!)
}
}
方法很简单:我相信它就是Master Detail模板中使用的方法。不同之处在于自定义 class.
有什么想法吗?
看起来 tableView.cellForRowAtIndexPath(indexPath!)
正在返回一个 UITableViewCell
。在将它传递给您的方法之前,您可能需要使用 as! CustomTableViewCell
将其转换为您的自定义类型。
我目前正在尝试编写 table 行删除(来自 Core Data)。我正在使用名为 "CustomTableViewCell" 的自定义 class。一切正常,直到我从以前的应用程序中实现了一个片段,该片段基本上只是添加了滑动删除功能。我现在收到以下错误:
无法将类型 'UITableViewCell' 的值转换为预期的参数类型 'CustomTableViewCell'
违规代码如下:
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!)
}
}
方法很简单:我相信它就是Master Detail模板中使用的方法。不同之处在于自定义 class.
有什么想法吗?
看起来 tableView.cellForRowAtIndexPath(indexPath!)
正在返回一个 UITableViewCell
。在将它传递给您的方法之前,您可能需要使用 as! CustomTableViewCell
将其转换为您的自定义类型。