我知道这个错误已经发布了很多次了。
问题是用户在调用 deleteRowsAtIndexPaths:withRowAnimation
之前忽略了从他们的数据数组中删除对象。或者有时,他们同时调用 reloadData
,然后调用 deleteRowsAtIndexPaths:withRowAnimation
。
但是,在调用 deleteRowsAtIndexPaths:withRowAnimation
之前,我确实从我的数据源(NSFetchedResultsController
)中删除了对象。而且我不调用 reloadData
。
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
/*Only allow deletion for collection table */
if(_segmentedControl.selectedSegmentIndex == 1) {
NSLog(@"delete ca");
if (editingStyle == UITableViewCellEditingStyleDelete)
{
CollectedLeaf* collectedLeaf = [collectionFetchedResultsController objectAtIndexPath:indexPath];
LeafletPhotoUploader * leafletPhotoUploader = [[LeafletPhotoUploader alloc] init];
leafletPhotoUploader.collectedLeaf = collectedLeaf;
if([LeafletUserRegistration isUserRegistered]) {
[leafletPhotoUploader deleteCollectedLeaf:collectedLeaf delegate:self];
}
// Delete the managed object for the given index path
NSManagedObjectContext *context = [collectionFetchedResultsController managedObjectContext];
[context deleteObject:[collectionFetchedResultsController objectAtIndexPath:indexPath]];
// Save the context.
NSError *error;
if (![context save:&error])
{
NSLog(@"Failed to save to data store: %@", [error localizedDescription]);
NSArray* detailedErrors = [[error userInfo] objectForKey:NSDetailedErrorsKey];
if(detailedErrors != nil && [detailedErrors count] > 0)
{
for(NSError* detailedError in detailedErrors)
{
NSLog(@" DetailedError: %@", [detailedError userInfo]);
}
}
else
{
NSLog(@" %@", [error userInfo]);
}
}
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
}
}
但是我仍然得到这个错误:
Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 1 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).
我尝试调用 reloadData
而不是 deleteRowsAtIndexPaths:withRowAnimation
。
虽然它解决了错误,但表格单元格并没有真正被删除,您可以通过仍然存在的“已收集:空”标签看到这一点:
这个 View Controller 有一个分段控件,它的索引改变了加载到表中的数据。数据从 NSFetchedResultsController
if(_segmentedControl.selectedSegmentIndex == 1) {
/// [_table removeFromSuperview];
_search_bar.hidden=YES;
UIImage *btnImage2 = [UIImage imageNamed:seg2_buttonImg];
[_left_button setImage:btnImage2 forState:UIControlStateNormal];
NSError *error;
[self.collectionFetchedResultsController performFetch:&error];
[self collectionFetchedResultsController];
collectedLeafArray = [collectionFetchedResultsController fetchedObjects];
[_table reloadData];
}
因此,这是我对 numberOfRowsInSection 的实现:
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if(_segmentedControl.selectedSegmentIndex == 0){
id <NSFetchedResultsSectionInfo> sectionInfo = [[speciesFetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
id <NSFetchedResultsSectionInfo> sectionInfo = [[collectionFetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
编辑: 删除行后如何更新 collectionFetchedResultsController 的计数?
最佳答案
I found a solution here .本质上,我是在数据源有时间完成删除之前调用 deleteRowsAtIndexPaths:withRowAnimation
。
对于其他在 UITableView 方面苦苦挣扎的人,这里有一个处理它的委托(delegate)方法和 here is the Apple documentation it was taken from :
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[self configureCell:[tableView cellForRowAtIndexPath:indexPath]
atIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
关于ios - 在调用 `deleteRowsAtIndexPaths:withRowAnimation` 之前删除对象仍然生成 : Invalid update: invalid number of rows in section 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46498193/