NSFetchedResultsController,在objective-c中更新表视图

时间:2015-11-24 11:16:29

标签: ios objective-c core-data

我正在尝试在核心数据中插入和删除数据。但是,在我的tableview中更新时我遇到了错误。 插入正常工作。在删除时,面临一些问题。问题如下:

错误:

*** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /SourceCache/UIKit_Sim/UIKit-3347.44.2/UITableView.m:1623
2015-11-24 16:21:18.775 datasApp[1898:91463] CoreData: error: Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out). with userInfo (null)
2015-11-24 16:21:18.801 datasApp[1898:91463] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 0.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (3), plus or minus the number of rows inserted or deleted from that section (1 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
***

CODING:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableView = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:

     NSLog(@"Insrt_before_tblNum_Row--->   %ld",(long)[tableView numberOfRowsInSection:newIndexPath.section]);
     [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];

     NSLog(@"Insrt_aftr_tblNum_Row--->   %ld",(long)[tableView numberOfRowsInSection:newIndexPath.section]);

break;

case NSFetchedResultsChangeDelete:

     //THIS LINE WORKING - TABLEVIEW HAVING 4 ROWS
     NSLog(@"delete_before_tblNum_Row--->   %ld",(long)[tableView numberOfRowsInSection:newIndexPath.section]);  
     //GETTING CRASH IN THIS 
     [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; LINE
     NSLog(@"delete_aftr_tblNum_Row--->   %ld",(long)[tableView numberOfRowsInSection:newIndexPath.section]);
break;

case NSFetchedResultsChangeUpdate:
     [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:[indexPath row] inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
     NSLog(@"Update");
     break;

 case NSFetchedResultsChangeMove:
      [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
      [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
 break;
}
}

0 个答案:

没有答案