1

我的应用程序使用了一个核心数据管理的表视图,我在 cimgf.com 的一些好人的帮助下实现了一种重新排序内容的方法。在此之前,我可以使用以下代码轻松删除对象:

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

    if (editingStyle == UITableViewCellEditingStyleDelete) {

        [self.tableView beginUpdates]; // Avoid  NSInternalInconsistencyException

        // Delete the person object that was swiped
        Step *stepToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
        NSLog(@"Deleting (%@)", stepToDelete.name);
        [self.managedObjectContext deleteObject:stepToDelete];
        [self.managedObjectContext save:nil];

        // Delete the (now empty) row on the table
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self performFetch];

        [self.tableView endUpdates];

        [delegate stepChangedOnMaster:self];
    }
}

添加以下代码后,当删除对象时,应用程序崩溃。

- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
      toIndexPath:(NSIndexPath *)destinationIndexPath;
{
    NSMutableArray *things = [[__fetchedResultsController fetchedObjects] mutableCopy];

    // Grab the item we're moving.
    NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];

    // Remove the object we're moving from the array.
    [things removeObject:thing];

    // Now re-insert it at the destination.
    [things insertObject:thing atIndex:[destinationIndexPath row]];

    // All of the objects are now in their correct order. Update each
    // object's displayOrder field by iterating through the array.
    int i = 0;
    for (NSManagedObject *mo in things)
    {
        NSLog(@"%i - %@", i, [mo valueForKey:@"name"]);
        [mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"];
    }
    things = nil;
    [__managedObjectContext save:nil];
    // Save
      NSError *error = nil;
    if (![__managedObjectContext save:&error])
    {
        NSString *msg = @"An error occurred when attempting to save your user profile changes.\nThe application needs to quit.";
        NSString *details = [NSString stringWithFormat:@"%@ %s: %@", [self class], _cmd, [error userInfo]];
        NSLog(@"%@\n\nDetails: %@", msg, details);
    }

    // re-do the fetch so that the underlying cache of objects will be sorted
    // correctly
    NSError *errror = nil;
    if (![__fetchedResultsController performFetch:&errror])
    {
        NSLog(@"Unresolved error %@, %@", errror, [errror userInfo]);
        abort();
    }
    [self.tableView reloadData];
}

我真的不明白为什么应用程序崩溃。有人可以给我一些关于如何解决这个问题的提示吗?谢谢!

这是我得到的错误: *由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (0) 必须相等更新前该节中包含的行数 (1),加上或减去从该节插入或删除的行数(0 插入,2 删除),加上或减去移入或移出该节的行数部分(0 移入,0 移出)。*

4

1 回答 1

1

在您调用之前,deleteRowsAtIndexPath您需要同步您的模型数据以对应于删除操作后您将在表视图中拥有的项目数,在您的情况下为 n-1。尽管您将其从持久存储中删除,但该项目可能仍缓存在数组中,该数组计算您在表视图的 section 方法中返回的行数。

于 2013-02-18T12:57:27.270 回答