【发布时间】:2014-05-21 13:29:02
【问题描述】:
我有一个带有部分的表格视图。当我尝试从表视图中删除一行时,出现以下错误............
'无效更新:第 5 节中的行数无效。更新后现有节中包含的行数 (4) 必须等于更新前该节中包含的行数 (4 ),加上或减去从该节插入或删除的行数(0 插入,1 删除),加上或减去移入或移出该节的行数(0 移入,0 移出)。' em>
经过一些在线研究,我了解到我必须从数组中删除对象。 我正在使用标准 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];
}
【问题讨论】:
-
你能展示你的
numberOfRowsInSection实现吗? -
感谢您的回复。我用更多代码更新了我的问题。
-
我对 NSFetchResultsController 不是很熟悉,但我想说你应该刷新 self.fetchedResultsController 或再次执行请求。首先你是如何创建它的?
标签: ios objective-c arrays uitableview core-data