我在动画添加或删除 UITableView 中的行时遇到问题,该行的高度与其他行不同。
以下 gif 演示了默认高度 (44pts) 行和插入和删除更大行 (100pts) 的问题。左边是模拟器的屏幕录像(新单元格最终覆盖第五行是一个不同的问题),右边是它应该做什么的模型。


就我而言,我有一堆行,每行 60pts 高。当点击单元格中的按钮时,“编辑”单元格将从下方滑出,将下方的单元格向下推。此编辑单元格高 180 点。当我调用insertRowsAtIndexPaths:withRowAnimation:ordeleteRowsAtIndexPaths:withRowAnimation:时,动画假定错误高度为 60pts,而不是 180pts 它应该是
这意味着在UITableViewRowAnimationTop新单元格出现在距离它最终位置的 -60pts 的情况下,并向下滑动到新的位置; 大约三分之一的动画应该做。同时,下面的行从其起始位置平滑地动画到向下 180 点,完全符合其应有的效果。
有没有人为此制定了实际的解决方案?某种方式告诉新行动画应该是什么高度?
下面是我用来隐藏和显示编辑行的代码。我正在使用 aTLSwipeForOptionsCell来触发编辑,但它很容易复制使用例如tableView:didSelectRowAtIndexPath:
-(void)hideEditFields{
    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForItem:editFormVisibleForRow+1 inSection:0]] withRowAnimation:UITableViewRowAnimationTop];
    editFormVisibleForRow = -1;
    [self.tableView endUpdates];
}
-(void)cellDidSelectMore:(TLSwipeForOptionsCell *)cell{
    NSIndexPath* indexPath = [self.tableView indexPathForCell:cell];
    // do nothing if this is the currently selected row
    if(editFormVisibleForRow != indexPath.row){
        if(editFormVisibleForRow >= 0){
            [self hideEditFields];
            // update the index path, as the cell positions (may) have changed
            indexPath = [self.tableView indexPathForCell:cell];
        }
        [self.tableView beginUpdates];
        editFormVisibleForRow = indexPath.row;
        [self.tableView insertRowsAtIndexPaths:@[
                                             [NSIndexPath indexPathForItem:editFormVisibleForRow+1 inSection:0]
                                             ] withRowAnimation:UITableViewRowAnimationTop];
        [self.tableView endUpdates];
    }
}
-(NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSource.count + (editFormVisibleForRow >= 0 ? 1 : 0);
}
-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    int row = indexPath.row;
    if(editFormVisibleForRow >= 0 && row > editFormVisibleForRow && row <= editFormVisibleForRow + 1){
        return 180.0f;
    }
    else return 60.0;
}
戳了一下,似乎这是一个没有明确答案的常见问题。我在这里找到的关于 SO 的大多数类似问题都没有得到解答,或者提供了针对提问者情况的解决方法。(示例:RowAnimation 问题、自定义 UITableViewCell 高度产生不正确的动画、删除和插入具有不同高度的单元格时 UITableView 动画故障)。
另外,我没有尝试制作一个三倍大小的编辑行,而是尝试制作三个较小的行并对其进行动画处理,但这并不合适,因为它们都同时出现。我也尝试一个接一个地为它们设置动画,但缓动让它看起来很奇怪,出现了一个明显的 3 步动画,而不是整个编辑视图在一个动作中滑出视图。
编辑:我刚刚注意到,如果我调用reloadRowsAtIndexPaths:withRowAnimation: UITableViewRowAnimationNone要设置动画的行上方的行,它会改变动画的行为;即动画假设高度为0pts,如下动画所示。它更接近我想要的,但仍然不正确,因为动画速度是错误的并且留下了差距(在我的应用程序中,这意味着背景颜色会穿透)
