swalkner
swalkner

Reputation: 17329

NSFetchedResultsController: problems when having two table rows for one object

I've got a tableView where each data set has two rows, therefore my datasource-methods look like the following:

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.fetchedResultsController.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectionIndex {
    id <NSFetchedResultsSectionInfo> section = self.fetchedResultsController.sections[sectionIndex];
    return section.numberOfObjects * 2;
}

The NSFetchedResultsControllerDelegate method:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    if (NSFetchedResultsChangeDelete == type) {
        NSIndexPath *cellIndexPath = indexPath;
        NSIndexPath *separatorIndexPath = [NSIndexPath indexPathForRow:indexPath.row + 1 inSection:indexPath.section];
        [self.transactionTableView deleteRowsAtIndexPaths:@[cellIndexPath, separatorIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else if (NSFetchedResultsChangeInsert == type) {
        NSIndexPath *cellIndexPath = newIndexPath;
        NSIndexPath *separatorIndexPath = [NSIndexPath indexPathForRow:newIndexPath.row + 1 inSection:newIndexPath.section];
        [self.transactionTableView insertRowsAtIndexPaths:@[cellIndexPath, separatorIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    } else {
        [self.transactionTableView reloadData];
    }
}

If I only use one row for each dataset, everything works as expected. But as soon as I make it this way, I get NSAssertionErrors here:

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

Isn't it possible to use two rows for one data set? What am I doing wrong? I would have expected to work it this way as I always insert two indexPaths whenever the underlying core data changes.

Upvotes: 0

Views: 228

Answers (1)

Martin R
Martin R

Reputation: 539715

That should definitely be possible, but your mapping from the FRC index path to the table view index path looks wrong. It should probably be

NSIndexPath *cellIndexPath = [NSIndexPath indexPathForRow:(2*indexPath.row) inSection:indexPath.section];
NSIndexPath *separatorIndexPath = [NSIndexPath indexPathForRow:(2*indexPath.row + 1) inSection:indexPath.section];

Upvotes: 1

Related Questions