繁体   English   中英

重新加载没有滚动或动画的 tableview 部分

[英]Reload tableview section without scroll or animation

我正在使用此代码重新加载 tableView 部分 -

self.tableView.beginUpdates()
self.tableView.reloadSections(NSIndexSet(index: 1), withRowAnimation: UITableViewRowAnimation.None)
self.tableView.endUpdates()

行替换仍然是动画的,表格视图滚动到顶部。

如何确保没有动画将应用于重新加载部分 + 防止表格视图滚动。

要防止滚动和动画,请执行以下操作:

UIView.performWithoutAnimation { 
    let loc = tableView.contentOffset
    tableView.reloadRows(at: [indexPath], with: .none)
    tableView.contentOffset = loc
}

斯威夫特4

实际上,在滚动或展开/折叠单元格期间防止疯狂动画的最佳方法是:

UIView.performWithoutAnimation {
       self.tableView.reloadRows(at: [indexPath], with: .none)
}

尝试这个:

UIView.setAnimationsEnabled(false)
self.tableView.beginUpdates()
self.tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: UITableViewRowAnimation.none)
self.tableView.endUpdates()

斯威夫特 5

UIView.performWithoutAnimation {
   self.tableView.reloadRows(at: [indexPath], with: .none)
  }

这两个答案都没有给我带来帮助。 这是我找到的解决方案,希望它对某人有用

斯威夫特4:

let lastScrollOffset = tableView.contentOffset

tableView.beginUpdates()
tableView.reloadSections(IndexSet(integer: 1), with: .none)
tableView.endUpdates()

tableView.layer.removeAllAnimations()
tableView.setContentOffset(lastScrollOffset, animated: false)

Objective-C的:

[UIView setAnimationsEnabled:NO];
   [[self tableView]beginUpdates];
   [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
   [[self tableView]endUpdates];

但我认为以下代码更有用,更好。

[UIView performWithoutAnimation:^{     
                [[self tableView]beginUpdates];
                [self.tableView reloadSections:[[NSIndexSet alloc] initWithIndex:1] withRowAnimation:UITableViewRowAnimationNone];
                [[self tableView]endUpdates];
            }];

在viewDidLoad中为此方法添加一行

self.tableView.remembersLastFocusedIndexPath = true

将此代码添加到更新后要刷新的位置

UIView.setAnimationsEnabled(false)
    self.tableView.beginUpdates()
    self.tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: UITableViewRowAnimation.none)
    self.tableView.endUpdates()

我也遇到过这个问题。 它似乎源于在 .reloadSections 中放置一个 IndexPath 而不是它请求的 IndexSet 。 这就是为什么它似乎可以与 .reloadRows 一起使用而没有问题:

tableView.reloadRows(at: [IndexPath], with: UITableView.RowAnimation)

tableView.reloadSections(IndexSet, with: UITableView.RowAnimation)

只需将要重新加载的部分包装为 IndexSet:

tableView.reloadSections(IndexSet(integer: sectionInt), with: .none)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM