繁体   English   中英

自定义UITableViewCell高度会产生不正确的动画

[英]Custom UITableViewCell height yields improper animation

我目前正在使用uitableview,该视图主要以44pts的标准尺寸存储单元。 但是,有一些较大,约为160点。

在这种情况下,有2行高度为44pts,其中较大的160pts行插入在下面的该部分的索引2处。

移除电话:

- (void)removeRowInSection:(TableViewSection *)section atIndex:(NSUInteger)index {
    NSUInteger sectionIndex = [self.sections indexOfObject:section];

    NSIndexPath *removalPath = [NSIndexPath indexPathForRow:index inSection:sectionIndex];

    [self.tableView beginUpdates];
    [self.tableView deleteRowsAtIndexPaths:@[removalPath] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView endUpdates];
}

代表致电:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    TableViewSection *section = [self sectionAtIndex:indexPath.section];

    return [section heightForRowAtIndex:indexPath.row];
}

部分调用:

- (NSInteger)heightForRowAtIndex:(NSInteger)index {
    StandardCell *cell = (StandardCell *)[self.list objectAtIndex:index];

    return cell.height;
}

手机呼叫:

- (CGFloat)height {
    return 160;
}

我感到困惑的是,当我从表格中删除较大的行时,它们开始设置动画,移动到上方的行下方。 但是,当它们到达某个点(大约是动画过程的1/4)时,它们会消失而不是完成动画。

似乎表格以只有44点的概念为行设置了动画,然后一旦到达上方行下面44点的位置,便将其从表格中删除。 我忽略了哪些细节可以使表具有正确的概念来自动为行删除设置动画?

谢谢你的帮助。

更新:我尝试注释掉上面的height函数(它将覆盖返回44的默认值)。 这样就可以制作出没有跳跃的适当动画。 FWIW

解决此问题的一种方法是在删除之前将行高动画化为44。

//mark index paths being deleted and trigger `contentSize` update
self.indexPathsBeingDeleted = [NSMutableArray arrayWithArray:@[indexPath]];
[tableView beginUpdates];
[tableView endUpdates];

//delete row
[self.tableView deleteRowsAtIndexPaths:@[removalPath] withRowAnimation:UITableViewRowAnimationAutomatic];
[tableView endUpdates];

然后在你的heightForRowAtIndexPath

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.indexPathsBeingDeleted containsObject:indexPath]) {
        //return normal height if cell is being deleted
        [self.indexPathsBeingDeleted removeObject:indexPath];
        return 44;
    }
    if (<test for tall row>) {
        return 160;
    }
    return 44;
}

正在进行一些簿记,以跟踪删除的索引路径。 可能有更清洁的方法可以做到这一点。 这只是想到的第一件事。 这是一个工作示例项目

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM