我不想动画在开始更新,结束更新块为 uitableview?

我有一个使用自定义表单元格的 UITableView,每个单元格都有一个 UIWebView。

因为 UIWebView 需要很长时间才能加载,所以我希望不惜一切代价避免重新加载它们。 在某些情况下,我有所有的细胞载入,但他们的高度是混乱的。 因此,我需要在不触发“ cellForRow”函数的情况下“重新布局”表。

  1. 我肯定不能使用 reloadData... 因为它会再次加载单元格。
  2. 我试过 tableView.setNeedDisplay、 setNeedsLayout 等,它们都不能重新排列表单元格
  3. 它唯一的工作方式,是调用开始更新/终止更新块,这个块能够重新布局我的表,而不会触发 cellForRow!但是,我不想要动画!这块产生一个动画效果,但我不想它..。

我怎样才能解决我的问题?

36275 次浏览
[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];

I had to do the following for this to work:

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

One more way to do it using blocks

Obj-C

[UIView performWithoutAnimation:^{
[self.tableView beginUpdates];
[self.tableView endUpdates];
}];

Swift

UIView.performWithoutAnimation {
tableView.beginUpdates()
tableView.endUpdates()
}

I wanted to updated the cell height for section 5 and following code worked for me:

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)

I prefer to have a smooth transition:

CGPoint offset = self.tableView.contentOffset;
[UIView transitionWithView:self.tableView duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[self.tableView reloadData];
self.tableView.contentOffset = offset;
} completion:nil];

give it a try.

working on my project, but not a common solution.

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

In my case worked this:

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
tableView.beginUpdates()
tableView.endUpdates()
return true
}


func textViewDidChange(_ textView: UITextView) {
DispatchQueue.main.async {
self.tableView.beginUpdates()
self.tableView.endUpdates()
}
}

for OBJ-C

[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];

for swift

UIView.setAnimationsEnabled(false)
tableView.beginUpdates()
tableView.endUpdates()
UIView.setAnimationsEnabled(true)

iOS disable animations in UITableView

From iOS v11 you should use the performBatchUpdates method instead of beginUpdates/endUpdates

UIView.performWithoutAnimation {
self.tableView.performBatchUpdates {
//table view updates
}
}