2个NSArray在UITableView中用于动画插入/删除的区别

时间:2010-03-13 20:04:43

标签: cocoa-touch uitableview animation nsarray

在我的应用程序的某个时刻,我有一个内容发生变化的NSArray。这些内容显示在UITableView中。我试图找到一种方法来找到NSArray之前和之后的内容之间的差异,这样我就可以将正确的indexPaths传递给insertRowsAtIndexPaths:withRowAnimation:和deleteRowsAtIndexPaths:withRowAnimation:以便更改动画。有什么想法吗?

THX

2 个答案:

答案 0 :(得分:5)

这是我尝试过的,它似乎有用,如果有人有更好的东西,我很乐意看到它。

[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];

答案 1 :(得分:2)

2010年的这个问题是我在谷歌搜索时发现的。从iOS 5.0开始,我们现在也有-[UITableView moveRowAtIndexPath: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];
}

关于如何使用它的示例。假设您正在显示一个人员表,您将其保存在数组self.people中。人员显示的部分索引为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];
    });
}