2
我的UITableView與NSFetchedResultsController表與NSFetchedResultsController招行與添加部分
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:[self dataFetchRequest]
managedObjectContext:[MLCoreDataService sharedInstance].managedObjectContext
sectionNameKeyPath:@"checked"
cacheName:nil];
aFetchedResultsController.delegate = self;
我實現
- (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:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[(MLItemTableViewCell*)[tableView cellForRowAtIndexPath:indexPath] setItem:anObject];
break;
case NSFetchedResultsChangeMove:
[tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
break;
}
[self.tableView reloadData];
}
當我改變我行「檢查」實體財產我接到一個電話到
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
帶有NSFetchedResultsChangeMove
當編輯之前和之後有相同的部分數量時,所有的都是正確的。 但是,當我只有一個部分,編輯一行應該去下一節後,我得到了以下錯誤
2014-07-27 17:37:43.384 mlist[34340:60b] 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 sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted). with userInfo (null)
2014-07-27 17:37:43.420 mlist[34340:60b] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of sections. The number of sections contained in the table view after the update (2) must be equal to the number of sections contained in the table view before the update (1), plus or minus the number of sections inserted or deleted (0 inserted, 0 deleted).'
當我嘗試插入刪除didChangeObject ......方法與類型的NSFetchedResultsChangeMove我有同樣的錯誤與部分中的行數。
有沒有常見的解決方法?什麼是正確的做法呢?
爲什麼你需要有_sectionsChanged布爾整個代碼定製呼叫解決?根據Apple的文檔和示例,當您有NSFetchedResultsChangeMove時,您只需要執行moveRowAtIndexPath。那裏有問題嗎? – Mikael
@Mikael _sectionsChanged只是爲了使用出現/消失的註釋 – Max