调用deleteRowsAtIndexPaths时,具有自定义大小单元格的UITableView变得生涩 [英] UITableView with self sizing cells becomes jerky when calling deleteRowsAtIndexPaths

查看:84
本文介绍了调用deleteRowsAtIndexPaths时,具有自定义大小单元格的UITableView变得生涩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有自定义大小单元格的UITableView.表包含1个部分和一些行.我有一些行说30,我想在didSelectRowAtIndexPath期间删除行.

I have a UITableView with self sizing cells. Table contains 1 section and some rows. I have some rows say 30 and I want to delete row during didSelectRowAtIndexPath.

当我删除tableview底部附近的可用行(例如第28行)时,第28行之前的行会更改其位置,并由于deleteRowsAtIndexPaths导致抖动的动画.有关更多详细信息,请参见附件的gif.

When I delete a row available near the bottom of tableview (say 28th row ), the rows before 28th row changes their position and it causes jerky animation as a result of deleteRowsAtIndexPaths. See attached gifs for more details.

使用帧删除动画构建的单元格

自动调整大小的单元格删除动画

  - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  {

       return 1;
  }
  -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  {
    return UITableViewAutomaticDimension;
  }

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
  return noOfRows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{
    CustomCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    [cell setCellText:[NSString stringWithFormat:@"Section = %li Row = %li", indexPath.section,indexPath.row]];

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    noOfRows--;
    [tableView deleteRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:indexPath.row inSection:0]] withRowAnimation:UITableViewRowAnimationFade];
}

此生涩的动画仅在自调整大小的单元格中发生,而不在使用框架构造的单元格中发生. 任何帮助将不胜感激.

This jerky animation happens only with self sizing cells and not with cells constructed with frame. Any help would be greatly appreciated.

问题是内容偏移量和内容大小.假设表格视图中的每个单元格的高度为50.因此,对于20个单元格,内容大小将为1000左右.假设设备高度为200,宽度为100.因此,内容大小将为100,1000.当我滚动到tableview的底部时,偏移量变为0,800.现在,当我删除一行时,内容大小将更新为100、950,并且内容偏移量现在会尝试根据内容大小的变化调整为新值.因此,这会导致发呆.

EDIT 1: The problem is content offset and content size. Lets say each cell in table view has height of 50. So for 20 cells the content size would be around 1000. Lets consider device height as 200 and width as 100.So the content size will be 100,1000. When I scroll to bottom of the tableview content offset becomes 0,800. When I delete a row now the content size will be updated to 100, 950 and content offset now tries to adjust to new value as a result of change in content size. So this causes Jerkiness.

推荐答案

生涩的动画是由于单元格的估计高度不正确造成的.因此,您需要在表视图控制器中实现estimatedRowHeight并返回保存的高度.您可以将先前加载的单元格高度在willDisplayCell tableView委托方法中保存为数组中的cell.frame.size.height.

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

        }

}

还将删除代码放入[tableView beginUpdates];[tableView endUpdates];

这篇关于调用deleteRowsAtIndexPaths时,具有自定义大小单元格的UITableView变得生涩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆