ChatGPT解决这个技术问题 Extra ChatGPT

我不想在开始更新、结束更新块中为 uitableview 设置动画?

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

因为 UIWebView 需要时间来加载,所以我想不惜一切代价避免重新加载它们。在某些情况下,我已加载所有单元格,但它们的高度搞砸了。因此,我需要在不触发“cellForRow”函数的情况下“重新布局”表格。

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

我该如何解决我的问题?


E
Evgeny Shurakov
[UIView setAnimationsEnabled:NO];
[tableView beginUpdates];
[tableView endUpdates];
[UIView setAnimationsEnabled:YES];

这很棒!我发现它有一些最有趣的应用。我发现即使你传入 UITableViewRowAnimationNone,reload section 方法也总是会产生动画效果,但这样可以轻松绕过动画!
D
Dmitriy

使用块的另一种方法

对象-C

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

迅速

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

h
hstdt

在我的项目上工作,但不是一个通用的解决方案。

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

N
Neil Japhtha

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) 放在完成块内很重要。
但这不会更新页脚,直到您滚动表格视图。
@Mr.Bean 完整的案例是什么? (即你的tableview总是有一个页脚,你是要插入一个还是删除一个?)
总是有一个页脚,只需要更新页脚(即页脚内有2个视图,需要根据情况隐藏/取消隐藏视图。)
@Mr.Bean 您的更新块包含什么?这里最可扩展的方法是重新加载该部分。
J
Jorge Arimany

我更喜欢平稳过渡:

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

试试看。


A
Aadi007

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

d
dimaskpz

用于 OBJ-C

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

为了迅速

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

u
user16658136

就我而言,这是这样的:

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

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

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅