我正在使用一个NSFetchedResultsController来显示我的核心数据,并且我在一个无处不在的文档容器中有一个用户生成的图像,对于fetchedResultsController显示的每个托管对象。
我已经在TableView上实现了向左滑动删除功能,但在fetchedResultsController删除我的对象之前,我在访问托管对象时遇到了问题。我还需要此访问权限来删除我的iCloud容器中用户生成的图像。
这是我正在使用的功能:
func controller(controller: NSFetchedResultsController!, didChangeObject anObject: AnyObject!, atIndexPath indexPath: NSIndexPath!, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath!) {
switch type
{
case .Insert:
tableView.insertRowsAtIndexPaths([newIndexPath], withRowAnimation: .Automatic)
case .Delete:
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
default:
break
}
}
我已经运行了测试,无法使用object、indexPath或newIndexPath引用已删除的对象。
如有任何协助,我们将不胜感激!谢谢Garret
为什么控制器要负责删除数据?如果其他控制器删除托管对象,他们也必须执行映像删除吗?这几乎不是一个好的解决方案。
我认为拦截托管对象子类中的删除并确保图像也被删除会更有意义。您可以使用托管对象的prepareForDeletion
挂钩。
您是否能够通过此回调截获表视图行删除命令?
// After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change
// Not called for edit actions using UITableViewRowAction - the action's handler will be invoked instead
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath;
正如标题注释所指出的,如果使用UITableViewRowActions,则无法使用此回调。
如果您使用的是行操作,那么我相信您应该能够在处理程序中拦截删除命令:
+ (instancetype)rowActionWithStyle:(UITableViewRowActionStyle)style title:(NSString *)title handler:(void (^)(UITableViewRowAction *action, NSIndexPath *indexPath))handler;