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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33338726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:05:52  来源:igfitidea点击:

Reload tableview section without scroll or animation

iosuitableview

提问by shannoga

I am reloading a tableView section using this code -

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

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

Still the rows replacement is animated and the table view scrolls to the top.

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

How can I make sure that no animation will be applied to the section reload + prevent the table view from scrolling.

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

回答by n13

To prevent scrolling and animation, do this:

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

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

回答by Alessandro Ornano

Swift 4:

斯威夫特 4

Actually, the best way to prevent crazy animation during scrolling or expand/collapse cells is:

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

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

回答by Abhinav

Try this:

尝试这个:

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

回答by Baradasy

Neither of given answers worked for me. Here's the solution I found, hope it would be useful to someone

给出的答案都不适合我。这是我找到的解决方案,希望对某人有用

Swift 4:

斯威夫特 4:

let lastScrollOffset = tableView.contentOffset

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

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

回答by Gurpreet Singh

Add one line more to this method in viewDidLoad

在 viewDidLoad 中为该方法多添加一行

self.tableView.remembersLastFocusedIndexPath = true

Add this code where you want refresh after updation

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

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

回答by Okan

Objective-C:

目标-C:

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

But I think the following code is more useful and better.

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

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

回答by Usama Ali

Swift 5

斯威夫特 5

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