3

我有一个通过 NSFetchedResultsController 填充的 UITableView。当用户向右滑动一个项目时,我会出现一个删除按钮,以便他/她可以使用以下方法删除该对象:

// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
    return YES;
}

 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {

if (editingStyle == UITableViewCellEditingStyleDelete) {
    //perform similar delete action as above but for one cell

    XMPPUserCoreDataStorageObject *user = [[self fetchedResultsController] objectAtIndexPath:indexPath];

    NSLog(@"User delete: %@", [user displayName]);

    //delete from fetchController
    NSArray *sections = [[self fetchedResultsController] sections];
    int userStatus = [[user sectionNum] intValue];


    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];

    }
}

但是当我这样做时会出现一个异常,因为我没有更新我的 fetchedResultsController 模型。问题是如何使用我从 commitEditingStyle 获得的 indexPath 从 fetchedController 中删除?

4

2 回答 2

7

由于您正在使用NSFetchedResultsController,您只需要从上下文中删除对象,它就会获取更改并为您删除行。

所以删除该deleteRowsAtIndexPaths:行并改为调用它:

[self.fetchedResultsController.managedObjectContext deleteObject:user];
于 2013-05-06T09:12:52.923 回答
1

看看这个答案,你需要做的是从你的数据库以及 NSFetchedResultsController 中删除一个 NSManagedObject。

于 2013-05-06T09:14:22.380 回答