调用reloadSections:withRowAnimation后,UITableView会跳转:

And*_*nko 8 uitableview uikit ios

我需要展开/折叠的一部分UITableView。我通过调用该节reloadSections:withRowAnimation:0tableView:numberOfRowsInSection:该节返回来实现此目的。

除了一种情况,它工作正常。如果表格视图一直向下滚动并且折叠后内容大小减小,则最终滚动超过最大偏移量。现在,它并没有在动画中滚动,而是在此处捕捉,看起来很丑陋:

在此处输入图片说明

这是我尝试过的:

  1. 包装reloadSections:withRowAnimation:beginUpdates/ endUpdates

    tableView.beginUpdates()
    tableView.reloadSections(indexSet, withRowAnimation: .Automatic)
    tableView.endUpdates()
    
    Run Code Online (Sandbox Code Playgroud)

    那没有帮助。

  2. 包裹reloadSections:withRowAnimation:CATransaction与完成块,计算在其内的滚动距离和滚动表视图动画:

    CATransaction.begin()
    CATransaction.setCompletionBlock {
        // tableView.contentSize is incorrect at this point
        let newContentOffset = ... // calculated value is not correct
        tableView.setContentOffset(newContentOffset, animated: true)
    }
    
    tableView.beginUpdates()
    tableView.reloadSections(indexSet, withRowAnimation: .Automatic)
    tableView.endUpdates()
    CATransaction.commit()
    
    Run Code Online (Sandbox Code Playgroud)

如何确保表格视图不会跳转?

小智 6

在可折叠部分中使用自定义标题时遇到了这个问题。我确实通过实施解决了它。

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat 
Run Code Online (Sandbox Code Playgroud)

  • 为我解决了这个问题! (2认同)

归档时间:

查看次数:

1285 次

最近记录:

7 年,10 月 前