当我尝试从包含多行的部分中删除一行时,当我向左滑动以显示删除按钮并按下它时,一旦刷新了tableView,它就会删除它,但在视觉上它什么都不做。如果我再按一次,它会删除该部分中的两行。如果我不再按它并按回然后返回到viewController,删除已经工作但动画从未发生过。删除只有一个单元格的部分中的单元格可以完美地工作。当部分中有2个或更多单元格时,委托函数永远不会被调用,但commitEditingStyle会调用。如果您需要更多代码,请告诉我们。感谢。
这是我的NSFetchedResultsControllerDelegate方法
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
self.context.deleteObject(object)
try! context.save()
}
func controllerWillChangeContent(controller: NSFetchedResultsController) {
}
func controllerDidChangeContent(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?)
{
var tableView = self.tableView as UITableView
switch(type)
{
case .Delete:
if let indexPath = indexPath
{
print("delete")
tableView.deleteRowsAtIndexPaths(NSArray(object:indexPath) as! [NSIndexPath], withRowAnimation: UITableViewRowAnimation.Fade)
}
break
case .Update:
if let indexPath = indexPath
{
if let cell = tableView.cellForRowAtIndexPath(indexPath)
{
cell.textLabel!.text = (self.fetchedResultsController.valueForKey("firstName") as! String) + " " + (self.fetchedResultsController.valueForKey("lastName") as! String)
print("update")
}
}
default:
break
}
}
func controller(controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType)
{
switch(type)
{
case .Delete:
tableView.deleteSections(NSIndexSet(index:sectionIndex), withRowAnimation: UITableViewRowAnimation.Fade)
break
default:
break
}
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
}
解决方案:我需要在commitEditingStyle函数的末尾添加一个self.tableView.reloadData()
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
let object = self.fetchedResultsController.objectAtIndexPath(indexPath) as NSManagedObject
self.context.deleteObject(object)
try! context.save()
self.tableView.reloadData()
}
答案 0 :(得分:1)
删除行后添加self.tableView.reloadData()
,或者技术上删除了行,但表视图尚未更新。请参阅下面的更新代码...
func controller(controller: NSFetchedResultsController,
didChangeSection sectionInfo: NSFetchedResultsSectionInfo,
atIndex sectionIndex: Int,
forChangeType type: NSFetchedResultsChangeType)
{
switch(type)
{
case .Delete:
//this creates the delete annimation
tableView.deleteSections(NSIndexSet(index:sectionIndex), withRowAnimation: UITableViewRowAnimation.Fade)
//this deletes the row from the array
self.array.removeAtIndex(indexPath.row)
//this reloads the table view so you can see the row deleted.
self.tableView.reloadData()
break
default:
break
}
}