Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference of 2 NSArray's for animated insert/delete in UITableView

At some point in my Application, I have an NSArray whose contents change. Those contents are shown in a UITableView. I'm trying to find a way to find the diff between the contents of before and after of the NSArray so i can pass the correct indexPaths to insertRowsAtIndexPaths:withRowAnimation: and deleteRowsAtIndexPaths:withRowAnimation: in order to have the changes nicely animated. Any ideas?

thx

like image 241
Alexander Cohen Avatar asked Mar 13 '10 20:03

Alexander Cohen


2 Answers

Here's is wat i tried and it seems to work, if anyone has anything better, i'd love to see it.

[self.tableView beginUpdates];

NSMutableArray* rowsToDelete = [NSMutableArray array];
NSMutableArray* rowsToInsert = [NSMutableArray array];

for ( NSInteger i = 0; i < oldEntries.count; i++ )
{
    FDEntry* entry = [oldEntries objectAtIndex:i];
    if ( ! [displayEntries containsObject:entry] )
        [rowsToDelete addObject: [NSIndexPath indexPathForRow:i inSection:0]];
}

for ( NSInteger i = 0; i < displayEntries.count; i++ )
{
    FDEntry* entry = [displayEntries objectAtIndex:i];
    if ( ! [oldEntries containsObject:entry] )
    [rowsToInsert addObject: [NSIndexPath indexPathForRow:i inSection:0]];
}

[self.tableView deleteRowsAtIndexPaths:rowsToDelete withRowAnimation:UITableViewRowAnimationFade];
[self.tableView insertRowsAtIndexPaths:rowsToInsert withRowAnimation:UITableViewRowAnimationRight];

[self.tableView endUpdates];
like image 107
Alexander Cohen Avatar answered Nov 20 '22 21:11

Alexander Cohen


This question from 2010 is what I found when I was googling. Since iOS 5.0, we now also have -[UITableView moveRowAtIndexPath:toIndexPath] which you really want to handle as well. Here is a function that compares two arrays and generates suitable indexpaths for the delete, insert and move operations.

- (void) calculateTableViewChangesBetweenOldArray:(NSArray *)oldObjects
                                         newArray:(NSArray *)newObjects
                                     sectionIndex:(NSInteger)section
                               indexPathsToDelete:(NSArray **)indexPathsToDelete
                               indexPathsToInsert:(NSArray **)indexPathsToInsert
                                 indexPathsToMove:(NSArray **)indexPathsToMove
                            destinationIndexPaths:(NSArray **)destinationIndexPaths
{

    NSMutableArray *pathsToDelete = [NSMutableArray new];
    NSMutableArray *pathsToInsert = [NSMutableArray new];
    NSMutableArray *pathsToMove = [NSMutableArray new];
    NSMutableArray *destinationPaths = [NSMutableArray new];

    // Deletes and moves
    for (NSInteger oldIndex = 0; oldIndex < oldObjects.count; oldIndex++) {
        NSObject *object = oldObjects[oldIndex];
        NSInteger newIndex = [newObjects indexOfObject:object];

        if (newIndex == NSNotFound) {
            [pathsToDelete addObject:[NSIndexPath indexPathForRow:oldIndex inSection:section]];
        } else if (newIndex != oldIndex) {
            [pathsToMove addObject:[NSIndexPath indexPathForRow:oldIndex inSection:section]];
            [destinationPaths addObject:[NSIndexPath indexPathForRow:newIndex inSection:section]];
        }
    }

    // Inserts
    for (NSInteger newIndex = 0; newIndex < newObjects.count; newIndex++) {
        NSObject *object = newObjects[newIndex];
        if (![oldObjects containsObject:object]) {
            [pathsToInsert addObject:[NSIndexPath indexPathForRow:newIndex inSection:section]];
        }
    }

    if (indexPathsToDelete)     *indexPathsToDelete =    [pathsToDelete copy];
    if (indexPathsToInsert)     *indexPathsToInsert =    [pathsToInsert copy];
    if (indexPathsToMove)       *indexPathsToMove =      [pathsToMove copy];
    if (destinationIndexPaths)  *destinationIndexPaths = [destinationPaths copy];
}

An example on how to use it. Assume you're displaying a table of people, which you keep in the array self.people. The section index where the people are displayed is 0.

- (void) setPeople:(NSArray <Person *> *)newPeople {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.tableView beginUpdates];

        NSArray *rowsToDelete, *rowsToInsert, *rowsToMove, *destinationRows;

        [self calculateTableViewChangesBetweenOldArray:self.people
                                              newArray:newPeople
                                          sectionIndex:0
                                    indexPathsToDelete:&rowsToDelete
                                    indexPathsToInsert:&rowsToInsert
                                      indexPathsToMove:&rowsToMove
                                 destinationIndexPaths:&destinationRows
         ];

        self.people = newPeople;

        [self.tableView deleteRowsAtIndexPaths:rowsToDelete withRowAnimation:UITableViewRowAnimationFade];
        [self.tableView insertRowsAtIndexPaths:rowsToInsert withRowAnimation:UITableViewRowAnimationFade];
        [rowsToMove enumerateObjectsUsingBlock:^(NSIndexPath *  _Nonnull oldIndexPath, NSUInteger idx, BOOL * _Nonnull stop) {
            NSIndexPath *newIndexPath = destinationRows[idx];
            [self.tableView moveRowAtIndexPath:oldIndexPath toIndexPath:newIndexPath];
        }];

        [self.tableView endUpdates];
    });
}
like image 32
skagedal Avatar answered Nov 20 '22 22:11

skagedal