How to use newIndexPath for NSFetchedResultsChangeUpdate?

I thought I got it . But the new crash that I found in my application says differently. So, does anyone know the really correct code for NSFetchedResultsChangeUpdatewhen newIndexPath is non-zero and does not match indexPath in -controller:didChangeObject:atIndexPath:forChangeType:newIndexPath:?

+5
source share
2 answers

I just encountered an update failure, and it seems to be newIndexPathprovided as an index path to the objects of the resulting result object when this index path does not match the index path needed to retrieve the cell from the table. Consider the following:

  • The table view has a section with index 0 with 15 elements.
  • 13 (- )
  • 14 ( )

, , [tableView beginUpdates] [tableView endUpdates] controllerWill/DidChangeContent:, indexPath ( 0, 14) newIndexPath ( 0, 13).

, , , , , , (- beginUpdates/endUpdates, ). , , , .

, , indexPath newIndexPath . , , , , nil newIndexPath, indexPath .

+8

NSFetchedResultsController " " -controller:didChangeObject:atIndexPath:forChangeType:newIndexPath: NSFetchedResultsChangeUpdate (. ).

, , , indexPath newIndexPath

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView;

    if (controller == self.fetchedResultsController) {
        tableView = self.tableView;
    }

    else {
        tableView = self.searchDisplayController.searchResultsTableView;
    }

    // type is "update" ---> should be "move"
    if (type == NSFetchedResultsChangeUpdate && [indexPath compare:newIndexPath] != NSOrderedSame && newIndexPath != nil) {
        type = NSFetchedResultsChangeMove;
    }

    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 fetchedResultsController:controller configureCell:(UITableViewCell*)[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
            [tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]withRowAnimation:UITableViewRowAnimationRight];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];

[self.tableView reloadData];
}

, !

+1

All Articles