ios - 奇怪的 UITableView 插入/删除行动画

标签 ios uitableview animation

在带有动画 (UITableViewRowAnimationTop) 的 UITableView 中插入/删除 UITableViewCell 时,我看到了一个奇怪的效果。

当要插入的单元格比上面的单元格大得多时,会出现动画故障。

This video显示模拟器中的故障,黄色单元格在应该从顶部滑动时突然出现。

Here是视频中的 Xcode 项目。

下面是单元格插入/动画代码。

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2 + self.thirdCellVisible;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if (indexPath.row == 1)
    {
        if (self.thirdCellVisible)
        {
            self.thirdCellVisible = !self.thirdCellVisible;
            [tableView deleteRowsAtIndexPaths:@[self.thirdCellIndexPath]
                             withRowAnimation:UITableViewRowAnimationTop];
        }
        else
        {
            self.thirdCellVisible = !self.thirdCellVisible;
            [tableView insertRowsAtIndexPaths:@[self.thirdCellIndexPath]
                             withRowAnimation:UITableViewRowAnimationTop];
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.thirdCellIndexPath.row)
    {
        return 100.0f;
    }

    return 44.0f;
}

最佳答案

你必须打电话

[tableView beginUpdates] 

[tableView endUpdates] 

调用插入/删除方法之前和之后如下:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2 + self.thirdCellVisible;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    if (indexPath.row == 1)
    {
        if (self.thirdCellVisible)
        {
            self.thirdCellVisible = !self.thirdCellVisible;
            [self.tableView beginUpdates];

            [tableView deleteRowsAtIndexPaths:@[self.thirdCellIndexPath]
                             withRowAnimation:UITableViewRowAnimationTop];

            [self.tableView endUpdates];

        }
        else
        {
            self.thirdCellVisible = !self.thirdCellVisible;
            [self.tableView beginUpdates];

            [tableView insertRowsAtIndexPaths:@[self.thirdCellIndexPath]
                             withRowAnimation:UITableViewRowAnimationTop];

            [self.tableView endUpdates];

        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.thirdCellIndexPath.row)
    {
        return 100.0f;
    }

    return 44.0f;
}

关于ios - 奇怪的 UITableView 插入/删除行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23021506/

相关文章:

ios - NSManagedObjectContext performBlockAndWait : and dispatch_semaphore causing a deadlock

ios - 无法在 iOS 设备上显示 UTF-8 字符

ios - swift 1.2 上的 RACCommand 编译失败

ios - 在 iOS 8+ 中使用什么字体显示这些 un​​icode 字符

iOS UIViewController,为什么viewDidUnload中 "self"变成 "wild pointer"?

ios - swift4 中的 UITextfield 验证 - 动态单元格

javascript - 如何使用 <g> 元素创建的路径对新的 svg 文件进行动画处理

swift - 如果一个tableView位于另一个tableView中,如何同时找到两个tableViewCell的indexPath?

java - 用于在 iOS/android 上为塔防等 2D 游戏制作动画的方法

java - Java2d 中的动画图像