Cocoa touch 2个NSArray和#x27的差异;用于在UITableView中插入/删除动画的

Cocoa touch 2个NSArray和#x27的差异;用于在UITableView中插入/删除动画的,cocoa-touch,uitableview,animation,nsarray,Cocoa Touch,Uitableview,Animation,Nsarray,在我的应用程序中的某个时刻,我有一个NSArray,它的内容会发生变化。这些内容显示在UITableView中。我正试图找到一种方法来查找NSArray的前后内容之间的差异,以便将正确的索引路径传递给insertRowsAtIndexPaths:withRowAnimation:和deleteRowsAtIndexPaths:withRowAnimation:以使更改具有良好的动画效果。有什么想法吗 thx这是我试过的水,它似乎很管用,如果有人有更好的,我很想看看 [self.tableView

在我的应用程序中的某个时刻,我有一个NSArray,它的内容会发生变化。这些内容显示在UITableView中。我正试图找到一种方法来查找NSArray的前后内容之间的差异,以便将正确的索引路径传递给insertRowsAtIndexPaths:withRowAnimation:和deleteRowsAtIndexPaths:withRowAnimation:以使更改具有良好的动画效果。有什么想法吗


thx

这是我试过的水,它似乎很管用,如果有人有更好的,我很想看看

[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];
[self.tableView开始更新];
NSMutableArray*rowsToDelete=[NSMutableArray];
NSMutableArray*rowsToInsert=[NSMutableArray];
对于(NSInteger i=0;i
2010年的这个问题是我在谷歌搜索时发现的。自iOS 5.0以来,我们现在还有
-[UITableView MoveRowatineXpath:toIndexPath]
,您也确实需要处理这些。这是一个比较两个数组并为删除、插入和移动操作生成适当索引路径的函数

- (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];
}
- (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];
    });
}