Reload tableview section without scroll or animation

后端 未结 8 871
轻奢々
轻奢々 2020-12-08 07:03

I am reloading a tableView section using this code -

self.tableView.beginUpdates()
self.tableView.reloadSections(NSIndexSet(index: 1), withRowAnimation: UITa         


        
相关标签:
8条回答
  • 2020-12-08 07:25

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

    Swift 4:

    let lastScrollOffset = tableView.contentOffset
    
    tableView.beginUpdates()
    tableView.reloadSections(IndexSet(integer: 1), with: .none)
    tableView.endUpdates()
    
    tableView.layer.removeAllAnimations()
    tableView.setContentOffset(lastScrollOffset, animated: false)
    
    0 讨论(0)
  • 2020-12-08 07:27

    To prevent scrolling and animation, do this:

    UIView.performWithoutAnimation { 
        let loc = tableView.contentOffset
        tableView.reloadRows(at: [indexPath], with: .none)
        tableView.contentOffset = loc
    }
    
    0 讨论(0)
  • 2020-12-08 07:33

    Add one line more to this method in 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()
    
    0 讨论(0)
  • 2020-12-08 07:37

    Swift 5

    UIView.performWithoutAnimation {
       self.tableView.reloadRows(at: [indexPath], with: .none)
      }
    
    0 讨论(0)
  • 2020-12-08 07:40

    Try this:

    UIView.setAnimationsEnabled(false)
    self.tableView.beginUpdates()
    self.tableView.reloadSections(NSIndexSet(index: 1) as IndexSet, with: UITableViewRowAnimation.none)
    self.tableView.endUpdates()
    
    0 讨论(0)
  • 2020-12-08 07:43

    I've had this problem too. It seems to stem from putting an IndexPath inside .reloadSections instead of a IndexSet as it requests. Which is why it seems to work with .reloadRows with out issue:

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

    Just wrap the section you want to reload as an IndexSet:

    tableView.reloadSections(IndexSet(integer: sectionInt), with: .none)
    
    0 讨论(0)
提交回复
热议问题