尝试将新表行插入节中的异常抛出

Nim*_*rod 1 iphone cocoa cocoa-touch core-data uitableview

我似乎遇到了一个非常奇怪的问题,只有在尝试将新的NSManagedObject插入新的部分时才会出现这个问题.基本上我的部分是几天,单个细胞与时间相关联.当我将一个对象移动到当前没有与该日期相关联的另一个对象(表格行)的那一天时,该表需要创建一个新的部分并移动该行.而不是正确发生这种情况我得到以下内容:

严重的应用错误.在调用-controllerDidChangeContent:期间,从NSFetchedResultsController的委托中捕获到异常. * - [NSMutableArray removeObjectAtIndex:]:索引1超出边界[0 .. 0] with userInfo(null)

插入一个当前没有其他对象的新对象似乎工作正常.

问题似乎在这个代码中的某个地方,但我似乎无法弄清楚它有什么问题. controller:didChangeSection:...似乎首先使用插入controller:didChangeObject:...调用,然后使用NSFetchedResultsChangeMove调用删除.所以顺序是:

NSFetchedResultsChangeInsert(在didChangeSection中)NSFetchedResultsChangeDelete(在didChangeSection中)NSFetchedResultsChangeMove(在didChangeObject中)

/**
 Delegate methods of NSFetchedResultsController to respond to additions, removals and so on.
 */

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller is about to start sending change notifications, so prepare the table view for updates.
[self.plainTableView beginUpdates];
}

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

switch(type) {
    case NSFetchedResultsChangeInsert:
        [tv insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeUpdate:
        [self configureCell:(UITableViewCell *)[tv cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
        break;

    case NSFetchedResultsChangeMove:
        [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        // Reloading the section inserts a new row and ensures that titles are updated appropriately.
        [tv reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        break;
}
}

- (void)controller:(NSFetchedResultsController *)controller
  didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex
     forChangeType:(NSFetchedResultsChangeType)type {
    switch(type) {
    case NSFetchedResultsChangeInsert:
        [self.plainTableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        break;

    case NSFetchedResultsChangeDelete:
        [self.plainTableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
        break;
}
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
// The fetch controller has sent all current change notifications, so tell the table view to process all updates.
[self.plainTableView endUpdates];
}
Run Code Online (Sandbox Code Playgroud)

我认为这只是标准的模板代码.有谁知道什么可能是错的?

实际上,当尝试将新行插入已存在的日期时,也会发生这种情况,但是在插入新行/对象的情况下不会存在.

Nim*_*rod 5

我似乎通过从最新版本的CoreDataBooks复制以下代码来解决这个问题:

    case NSFetchedResultsChangeMove:
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
        break;
Run Code Online (Sandbox Code Playgroud)

在此之前,这里的代码是:

    case NSFetchedResultsChangeMove:
        [tv deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        // Reloading the section inserts a new row and ensures that titles are updated appropriately.
        [tv reloadSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] withRowAnimation:UITableViewRowAnimationFade];
        break;
Run Code Online (Sandbox Code Playgroud)