1
我的应用程序使用Core Data托管的表视图,我在cimgf.com上的一些好人帮助下实现了一个重新排序内容的方法。 在这样做之前,我能够通过使用下面的代码很容易地删除对象:从Core Data managet中删除对象时出错表视图
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.tableView beginUpdates]; // Avoid NSInternalInconsistencyException
// Delete the person object that was swiped
Step *stepToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"Deleting (%@)", stepToDelete.name);
[self.managedObjectContext deleteObject:stepToDelete];
[self.managedObjectContext save:nil];
// Delete the (now empty) row on the table
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self performFetch];
[self.tableView endUpdates];
[delegate stepChangedOnMaster:self];
}
}
添加下面的代码,当一个对象被删除,应用程序崩溃之后。
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
- (void)tableView:(UITableView *)tableView
moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath
toIndexPath:(NSIndexPath *)destinationIndexPath;
{
NSMutableArray *things = [[__fetchedResultsController fetchedObjects] mutableCopy];
// Grab the item we're moving.
NSManagedObject *thing = [[self fetchedResultsController] objectAtIndexPath:sourceIndexPath];
// Remove the object we're moving from the array.
[things removeObject:thing];
// Now re-insert it at the destination.
[things insertObject:thing atIndex:[destinationIndexPath row]];
// All of the objects are now in their correct order. Update each
// object's displayOrder field by iterating through the array.
int i = 0;
for (NSManagedObject *mo in things)
{
NSLog(@"%i - %@", i, [mo valueForKey:@"name"]);
[mo setValue:[NSNumber numberWithInt:i++] forKey:@"displayOrder"];
}
things = nil;
[__managedObjectContext save:nil];
// Save
NSError *error = nil;
if (![__managedObjectContext save:&error])
{
NSString *msg = @"An error occurred when attempting to save your user profile changes.\nThe application needs to quit.";
NSString *details = [NSString stringWithFormat:@"%@ %s: %@", [self class], _cmd, [error userInfo]];
NSLog(@"%@\n\nDetails: %@", msg, details);
}
// re-do the fetch so that the underlying cache of objects will be sorted
// correctly
NSError *errror = nil;
if (![__fetchedResultsController performFetch:&errror])
{
NSLog(@"Unresolved error %@, %@", errror, [errror userInfo]);
abort();
}
[self.tableView reloadData];
}
我真的不明白为什么应用程序崩溃。有人可以给我一些关于如何解决这个问题的提示吗?谢谢!
这是错误我得到: *终止应用程序由于未捕获的异常“NSInternalInconsistencyException”,理由是:“无效的更新:在第0无效的行数后包含在现有的部分的行数update(0)必须等于更新前(1)的该部分中包含的行数,加上或减去从该部分插入或删除的行数(0插入,2删除)和正数或负数行移入或移出该部分(0移入,0移出)。' *
调试控制台提供的关于崩溃的内容是什么?应该有某种消息或其他。如果您设置了一个异常断点,那么在显示错误消息之前,您可能需要按一次或两次“coninue”。 – 2013-02-18 11:43:26
用错误更新了问题 – 2013-02-18 12:43:47