UITableView with self sizing cells becomes jerky when calling deleteRowsAtIndexPaths

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 11:05:00

The jerky animation is due to incorrect estimated heights of your cells. So you need to implement estimatedRowHeight in your table view controller and return saved heights. You can save previously loaded cell heights in willDisplayCell tableView delegate methods as cell.frame.size.height in an array.

var estimatedHeights : [CGFloat] = []

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat
{
        if estimatedHeights.count > indexPath.row
        {
            return estimatedHeights[indexPath.row]
        }

        return 40

}

func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath)
{


        if estimatedHeights.count <= indexPath.row
        {
            estimatedHeights.append(cell.frame.height)

        }
        else
        {
            estimatedHeights[indexPath.row] = cell.frame.height

        }

}

Also put your delete code within [tableView beginUpdates]; and [tableView endUpdates];

Update your method like this and try:

cellForRowAtIndexPath:

[cell setNeedsUpdateConstraints];
[cell layoutIfNeeded];

didSelectRowAtIndexPath:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    noOfRows--;
[tableView beginUpdates];
    [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
[tableView endUpdates];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!