4

我有一个 UITableViewController 管理一个分组的 tableView。tableView 由 fetchedResultsController 填充。

如果我单击 NavigationBar 中的Edit按钮,然后选择一行并单击Delete按钮,则该行被删除并且一切顺利。

但是,如果我连续滑动以显示“删除”按钮并单击“删除”按钮,应用程序将崩溃并出现以下错误:

2010-01-06 15:25:18.720 Take10 [14415:20b] 严重的应用程序错误。在核心数据更改处理期间捕获到异常:-[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)

2010-01-06 15:25:18.721 Take10 [14415:20b] 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“***-[NSCFArray objectAtIndex:]:索引 (1) 超出范围 (1)”

当然,错误中的索引号会根据我尝试删除行的部分中的行数而变化,并且该数字比尝试删除后表部分中剩余的行数多 1。

这是我尝试从 fetchedResultsController 中删除数据的代码。相同的方法对这两种情况都有反应,所以我不明白为什么在刷卡时它会崩溃。

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

if (editingStyle == UITableViewCellEditingStyleDelete) {
    // Delete the managed object for the given index path
    NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
    [context deleteObject:[fetchedResultsController objectAtIndexPath:indexPath]];
    
    // Save the context.
    NSError *error = nil;
    if (![context save:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.
         
         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }
  }   
}

有任何想法吗???

谢谢

杰克

4

2 回答 2

2

普通删除和滑动删除之间的一个区别是后者将调用tableView:willBeginEditingRowAtIndexPathand tableView:didEndEditingRowAtIndexPath。事实上,这是抑制插入行的缩进和显示的好方法。

另一个区别是在删除后立即setEditing:调用(NO作为参数值)。

在您定义/覆盖的这三个函数中的任何一个中设置断点,看看您是否可以缩小它发生的位置。

于 2010-01-07T01:33:30.313 回答
2

Jeff LaMarche 在 NSFetchedResultsController 方面做了一些很好的工作。

尝试在这里使用他的模板:http: //iphonedevelopment.blogspot.com/2010/01/navigation-based-core-data-application.html

看看这是否能解决你的问题。

于 2010-01-07T15:18:34.123 回答