我不想在uitableview的开始更新、结束更新块中显示动画



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

因为UIWebView需要很长时间才能加载,所以我想避免不惜一切代价重新加载它们。在某些情况下,我会装载所有的细胞,但它们的高度会被打乱。因此,我需要在不触发"cellForRow"函数的情况下"重新发布"该表。

  1. 我绝对不能使用reloadData。。。因为它将再次重新加载单元格
  2. 我试过tableView.setNeedDisplay、setNeedsLayout等,它们都无法重新排列表格单元格
  3. 它唯一的工作方式是调用beginupdates/endupdates块,这个块可以在不触发cellForRow的情况下重新发布我的表!但是,我不想要动画!这个块会产生动画效果,但我不想要它

我该如何解决我的问题?

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

使用块的另一种方法

Obj-C

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

Swift

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

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

正在处理我的项目,但不是一个通用的解决方案。

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

我更喜欢平稳过渡:

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

试试看。

iOS禁用UITableView中的动画

从iOS v11,您应该使用performBatchUpdates方法,而不是beginUpdates/endUpdates

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

我想更新第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()
    }
}

最新更新