我有一个使用自定义表格单元格的 UITableView,每个单元格都有一个 UIWebView。
因为 UIWebView 需要时间来加载,所以我想不惜一切代价避免重新加载它们。在某些情况下,我已加载所有单元格,但它们的高度搞砸了。因此,我需要在不触发“cellForRow”函数的情况下“重新布局”表格。
我绝对不能使用 reloadData... 因为它会再次重新加载单元格。我尝试了 tableView.setNeedDisplay、setNeedsLayout 等,它们都无法重新排列表格单元格它唯一起作用的方法是调用 beginupdates/endupdates 块,这个块能够在不触发 cellForRow 的情况下重新布局我的表格!但是,我不想要动画!这个块产生动画效果,但我不想要它......
我该如何解决我的问题?
[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];
使用块的另一种方法
对象-C
[UIView performWithoutAnimation:^{
[self.tableView beginUpdates];
[self.tableView endUpdates];
}];
迅速
UIView.performWithoutAnimation {
tableView.beginUpdates()
tableView.endUpdates()
}
在我的项目上工作,但不是一个通用的解决方案。
let loc = tableView.contentOffset
UIView.performWithoutAnimation {
tableView.reloadData()
tableView.layoutIfNeeded()
tableView.beginUpdates()
tableView.endUpdates()
tableView.layer.removeAllAnimations()
}
tableView.setContentOffset(loc, animated: true)//animation true may perform better
Swifties 我必须执行以下操作才能使其正常工作:
// Sadly, this is not as simple as calling:
// UIView.setAnimationsEnabled(false)
// self.tableView.beginUpdates()
// self.tableView.endUpdates()
// UIView.setAnimationsEnabled(true)
// We need to disable the animations.
UIView.setAnimationsEnabled(false)
CATransaction.begin()
// And we also need to set the completion block,
CATransaction.setCompletionBlock { () -> Void in
// of the animation.
UIView.setAnimationsEnabled(true)
}
// Call the stuff we need to.
self.tableView.beginUpdates()
self.tableView.endUpdates()
// Commit the animation.
CATransaction.commit()
setAnimationsEnabled(true)
放在完成块内很重要。
我更喜欢平稳过渡:
CGPoint offset = self.tableView.contentOffset;
[UIView transitionWithView:self.tableView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[self.tableView reloadData];
self.tableView.contentOffset = offset;
} completion:nil];
试试看。
我想更新第 5 节的单元格高度,以下代码对我有用:
UiView.setAnimationsEnabled(False)
self.productTableView.reloadSections(NSIndexSet(index: SectionType.ProductDescription.hashValue), withRowAnimation: UITableViewRowAnimation.None)
self.productTableView.scrollToRowAtIndexPath(NSIndexPath(forRow: 0, inSection: 5), atScrollPosition: UITableViewScrollPosition.Bottom, animated: false)
UIView.setAnimationsEnabled(true)
用于 OBJ-C
[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];
为了迅速
UIView.setAnimationsEnabled(false)
tableView.beginUpdates()
tableView.endUpdates()
UIView.setAnimationsEnabled(true)
就我而言,这是这样的:
func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
tableView.beginUpdates()
tableView.endUpdates()
return true
}
func textViewDidChange(_ textView: UITextView) {
DispatchQueue.main.async {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
}
UITableViewRowAnimationNone
,reload section 方法也总是会产生动画效果,但这样可以轻松绕过动画!