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

时间:2016-01-21 13:45:38

标签: ios uitableview uikit

我需要展开/折叠UITableView的一部分。我是通过调用reloadSections:withRowAnimation:并从0返回tableView:numberOfRowsInSection:来执行此操作的,如果该部分应该被折叠。

除一例外,它的工作正常。如果表视图完全向下滚动并且内容大小在折叠后减小,则最终会滚动超出最大偏移量。 现在不是用动画滚动到位,而只是在那里拍摄,看起来很难看:

enter image description here

以下是我的尝试:

  1. reloadSections:withRowAnimation: / beginUpdates中结束endUpdates

    tableView.beginUpdates()
    tableView.reloadSections(indexSet, withRowAnimation: .Automatic)
    tableView.endUpdates()
    

    这没有帮助。

  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()
    
  3. 如何确保表格视图不会跳转?

1 个答案:

答案 0 :(得分:2)

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat 
相关问题