這裏移動行時是個例外:崩潰中的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