我有一个包含剖面的表视图。当我尝试从表视图中删除一行时,我收到以下错误.........
'无效更新:第5节中的行数无效。更新后的现有部分中包含的行数(4)必须等于更新前该部分中包含的行数(4)加上或减去从该部分插入或删除的行数(0插入,1删除)和加或减移入或移出该部分的行数(0移入,0移出)。'
经过一些在线研究后,我明白我必须从数组中删除对象。 我使用标准managedObjectContext和fetchedResultsController的核心数据。我不明白这里的数组是什么。 有人可以帮忙吗。
以下是节数和行数的代码....
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
NSInteger count = [[self.fetchedResultsController sections] count];
return count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSInteger count = [sectionInfo numberOfObjects];
return count;
}
以下是我用来删除行的代码....
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the managed object at the given index path.
NSManagedObject *birthdayToDelete = [self.fetchedResultsController objectAtIndexPath:indexPath];
[self.managedObjectContext deleteObject:birthdayToDelete];
// Update the array and table view.
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
// Commit the change.
NSError *error = nil;
if (![self.managedObjectContext 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.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
[_birthdayTableView reloadData];
}
答案 0 :(得分:0)
这通常是因为您的数组中的项目数量与numberOfRowsInSection:
返回的数字不符,请确保您在编辑时正确调用beginUpdates
和endUpdates
。< / p>
答案 1 :(得分:0)
如果您使用[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:YES];
委托方法,则无需致电NSFetchResultsController
。实现以下内容:保存managedObjectContext
帖子删除后将调用这些内容。 (记得设置NSFetchedResultsController
的委托。另外,不要打电话给[tableView reloadData];
。它会让动画跳过并看起来很奇怪:-)这是使用NSFetchedResultsController
的美妙之处,它确实如此对你来说很重要,并倾听managedObjectContext
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationLeft];
break;
case NSFetchedResultsChangeMove:break;
case NSFetchedResultsChangeUpdate:break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationLeft];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationRight];
break;
case NSFetchedResultsChangeUpdate:
[tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationNone];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[tableView endUpdates];
}