这里移动行时是个例外:崩溃中的UITableView
Serious application error. Exception was caught during
Core Data change processing: *** -[NSCFArray removeObjectAtIndex:]:
index (0) beyond bounds (0) with userInfo (null)
下面是相关代码:
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
NSMutableArray *array = [[fetchedResultsController fetchedObjects] mutableCopy];
id objectToMove = [[array objectAtIndex:fromIndexPath.row] retain];
[array removeObjectAtIndex:fromIndexPath.row];
[array insertObject:objectToMove atIndex:toIndexPath.row];
[objectToMove release];
for (int i=0; i<[array count]; i++) {
[(NSManagedObject *)[array objectAtIndex:i] setValue:[NSNumber numberWithInt:i] forKey:JKChecklistRow];
}
[array release];
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
// Reloading the section inserts a new row and ensures that titles are updated appropriately.
[tableView reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
我有一个怀疑,这次金融危机是与核心数据商店与未保存的上下文不同步。如果我在-tableView:moveRowAtIndexPath:fromIndexPath:toIndexPath:
方法中保存上下文,程序崩溃得更快,但我还是无法弄清楚为什么。
哦,快点。对此解决方案的一个警告是,显然,通过轻扫即可删除将表视图置于编辑模式。我的if语句导致数据现在不被重新加载。修复了另一个问题:-) – kubi 2009-12-06 12:48:08