删除tableView中的行时出现核心数据错误
我有一个管理分组tableView的UITableViewController。 tableView从fetchedResultsController中填充。删除tableView中的行时出现核心数据错误
如果我点击导航栏的编辑按钮,然后选择一列并点击删除按钮,该行被删除,所有一切都好。
但是,如果我滑动显示连续的删除按钮,然后单击删除按钮,该应用程序崩溃,出现以下错误:
2010-01-06 15:25:18.720 Take10[14415:20b] Serious application error. Exception was caught during Core Data change processing: -[NSCFArray objectAtIndex:]: index (1) beyond bounds (1) with userInfo (null)
2010-01-06 15:25:18.721 Take10[14415:20b] Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFArray objectAtIndex:]: index (1) beyond bounds (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();
}
}
}
任何想法?
感谢
JK
Jeff LaMarche用NSFetchedResultsController做了一些很好的工作。
尝试使用此模板了: http://iphonedevelopment.blogspot.com/2010/01/navigation-based-core-data-application.html
,看看是否能解决您的问题。
一个正常的刷卡和删除的区别是,后者将调用tableView:willBeginEditingRowAtIndexPath
和tableView:didEndEditingRowAtIndexPath
。实际上,这是抑制插入行的缩进和显示的好方法。
另一个区别是在删除后立即调用setEditing:
(使用NO
作为参数值)。
在您定义/覆盖的三个函数中的任何一个中设置断点,并查看是否可以缩小它发生的位置。
您是否重新加载controllerDidChangeContent中的表:? – gerry3 2010-01-07 01:01:06
尝试打破objc_exception_throw并查看抛出异常的方法。这应该有助于追踪它。 – 2010-01-07 05:39:03