【发布时间】:2016-01-21 13:45:38
【问题描述】:
我需要展开/折叠UITableView 的一部分。我通过调用reloadSections:withRowAnimation: 并从tableView:numberOfRowsInSection: 返回0 来执行此操作,如果该部分应该被折叠。
除了一种情况外,它工作正常。如果表格视图一直向下滚动并且折叠后内容大小减小,则最终滚动超出最大偏移量。 现在不是用动画滚动到位,它只是卡在那里,看起来很难看:
这是我尝试过的:
-
将
reloadSections:withRowAnimation:包装在beginUpdates/endUpdates中:tableView.beginUpdates() tableView.reloadSections(indexSet, withRowAnimation: .Automatic) tableView.endUpdates()这没有帮助。
-
使用完成块将
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()
如何确保表格视图不跳转?
【问题讨论】:
-
我想我也面临同样的问题。 gif有点难以理解你在说什么。有什么方法可以创建一个短片吗?会给出很清晰的想法。
-
@rohan-patel 替换了demo,这个应该能更好的演示问题。
标签: ios uitableview uikit