【问题标题】:UITableView jumps after calling reloadSections:withRowAnimation: [duplicate]UITableView 在调用 reloadSections:withRowAnimation: [重复] 后跳转
【发布时间】:2016-01-21 13:45:38
【问题描述】:

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

除了一种情况外,它工作正常。如果表格视图一直向下滚动并且折叠后内容大小减小,则最终滚动超出最大偏移量。 现在不是用动画滚动到位,它只是卡在那里,看起来很难看:

这是我尝试过的:

  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()
    

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

【问题讨论】:

标签: ios uitableview uikit


【解决方案1】:

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

func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat 

【讨论】:

  • 帮我解决了这个问题!
猜你喜欢
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多