问题是,用户在调用之前忽略了从数据数组中删除对象
deleteRowsAtIndexPaths:withRowAnimation
reloadData
然后
deleteRowsAtIndexPaths:withRowAnimation
.
但是,我确实从数据源中删除了该对象(a)
NSFetchedResultsController
)通话前
deleteRowsAtIndexPaths:withRowAnimation
. 我不打电话
.
- (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];
}
}
}
无效更新:节0中的行数无效。数量
更新(2)后现有节中包含的行必须为
更新(2),加上或减去从中插入或删除的行数
该部分(插入0,删除1)加上或减去
移入或移出该节的行(0移入,0移出)。
而不是
.
虽然它解决了错误,但表单元格并没有真正被删除,您可以通过仍然存在的“Collected:null”标签看到:
此viewcontroller有一个分段控件,其索引更改加载到表中的数据。数据从
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的计数?