我觉得这可能是一个普遍的问题,并且想知道是否有任何共同的解决方案.
基本上,我的UITableView具有每个单元格的动态单元格高度.如果我不在UITableView和我的顶部tableView.reloadData(),向上滚动变得有点跳跃.
我相信这是因为我重新加载数据,因为我正在向上滚动,UITableView会重新计算每个进入可见性的单元格的高度.如何缓解这种情况,或者如何仅将数据从某个IndexPath重新加载到UITableView的末尾?
此外,当我设法一直滚动到顶部时,我可以向下滚动然后向上滚动,没有跳跃没问题.这很可能是因为已经计算了UITableViewCell高度.
问题:当我在底部的的UITableView(且仅当在底部)和tableView.beginUpdates()/ tableView.endUpdates()被调用时,UITableView跳了一点点.我不希望它这样做.
设置:我有一个UITableView与UITableViewCells将全部在不同的大小和我使用UITableViewAutomaticDimension大小的细胞.
示例:
当我在底部时UITableView,我选择一个单元格,随后调用tableView.beginUpdates()/ tableView.endUpdates(),UITableView滚动/跳起一点点.
有没有人找到一种方法来做到这一点,所以没有跳跃后呼叫tableView.beginUpdates()/ tableView.endUpdates()在结束时UITableView?
暂时我正在UITableViewCells手动计算我的全部,但我宁愿不这样做,因为我想利用它UITableViewAutomaticDimension.
代码:
这里有一些代码来显示tableView.beginUpdates()/ 之间发生的事情tableView.endUpdates():
tableView.beginUpdates()
cell!.setNeedsUpdateConstraints()
cell!.updateConstraintsIfNeeded()
cell!.setNeedsLayout()
cell!.layoutIfNeeded()
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)
我的观点会发现:
public override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tableView.estimatedRowHeight = 150.0
self.tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.reloadData()
//Bug in 8.0+ forces us to call three extra …Run Code Online (Sandbox Code Playgroud) 目前我有一个UITableView调整大小UITextView.单元格使用beginUpdates/ 自动调整大小endUpdates,但是当它执行时,表格视图会断断续续(请参阅下面的gif).
最终结果是在UITableViewCell其中具有基于其内容调整大小的文本视图.以下是自定义UITableViewCell类中导致UITableView更新自身的代码.
- (void)textViewDidChange:(UITextView *)textView {
// This is a category on UITableViewCell to get the [self superView] as the UITableView
UITableView *tableView = [self tableView];
if (tableView){
[tableView beginUpdates];
[tableView endUpdates];
}
}
Run Code Online (Sandbox Code Playgroud)
以下是我已经尝试过的事情:
获取当前contentOffset并重置它endUpdates但不起作用UITableView在更新之前禁用滚动然后启用我尝试NO从- (BOOL)textViewShouldEndEditing:(UITextView *)textView
我的UITableView单元格高度返回总是使用UITableViewAutomaticDimension.欢迎任何其他想法或想法.
以下是它的样子:

我不打算使用任何库,所以请不要提出任何建议.
谢谢
编辑:解决方案
在这里找到:我不希望动画在开始更新,结束更新块为uitableview?
感谢@JeffBowen获得了一个很好的发现(虽然hacky它是可行的并且允许我仍然实现UITableViewDelegate支持iOS 7 的方法).在执行更新之前关闭动画,然后在更新后启用以防止UITableView口吃. …
我需要展开/折叠的一部分UITableView。我通过调用该节reloadSections:withRowAnimation:并0从tableView:numberOfRowsInSection:该节返回来实现此目的。
除了一种情况,它工作正常。如果表格视图一直向下滚动并且折叠后内容大小减小,则最终滚动超过最大偏移量。现在,它并没有在动画中滚动,而是在此处捕捉,看起来很丑陋:
这是我尝试过的:
包装reloadSections:withRowAnimation:在beginUpdates/ endUpdates:
tableView.beginUpdates()
tableView.reloadSections(indexSet, withRowAnimation: .Automatic)
tableView.endUpdates()
Run Code Online (Sandbox Code Playgroud)
那没有帮助。
包裹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)如何确保表格视图不会跳转?