2

虽然这不是故障/错误,但它作为一个例外出现。当我尝试使用核心数据删除或插入对象时,第一次插入或删除显示以下错误:

2012-04-09 18:57:32.201 SEA[7437:fb03] * -[UITableView _endCellAnimationsWithContext:] 中的断言失败,/SourceCache/UIKit_Sim/UIKit-1912.3/UITableView.m:1046 2012-04-09 18:57: 32.203 SEA[7437:fb03] CoreData:错误:严重的应用程序错误。在调用 -controllerDidChangeContent: 期间,从 NSFetchedResultsController 的委托中捕获了一个异常。无效更新:第 0 节中的行数无效。更新后现有节中包含的行数 (5) 必须等于更新前该节中包含的行数 (5),加上或减去数字从该部分插入或删除的行数(1 插入,0 删除)加上或减去移入或移出该部分的行数(0 移入,0 移出)。与用户信息(空)

我有点不确定为什么会出现这种情况,因为实际的 tableView 显示了新行或删除的行。数据库还显示正确的值和所有内容。我正在使用的代码如下:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
    {
        return [[fetchedResultsController sections] count];
    }
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return [[[fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
    }

如果您需要查看更多代码,请告诉我。

在显示所有用户的 tableViewController 中。我有以下内容:

(void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView beginUpdates];
}
(void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.tableView endUpdates];
}

这是 commitEditingStyle 的代码

(void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the row from the data source

    // Get context and user to delete
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    User *userToDelete = [fetchedResultsController objectAtIndexPath:indexPath];

    // Delete user from context
    [context deleteObject:userToDelete];

    // Save!
    NSError *error = nil;
    if(![context save:&error]){
        // Handle the error..
    }

}   
else if (editingStyle == UITableViewCellEditingStyleInsert) {
    // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
  }   
}
4

0 回答 0