簡體   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