UITableView Refresh without scrolling

后端 未结 12 600
时光说笑
时光说笑 2021-02-03 23:06

I have a _TableView with items , and I want to set automatic refresh,and I don\'t want it to scroll on refresh , lets say user scrolled 2 pages down , and the refre

12条回答
  •  你的背包
    2021-02-03 23:42

    For Swift 3+:

    You need to save the current offset of the UITableView, then reload and then set the offset back on the UITableView.

    I have created this function for this purpose:

    func reload(tableView: UITableView) {
    
        let contentOffset = tableView.contentOffset
        tableView.reloadData()
        tableView.layoutIfNeeded()
        tableView.setContentOffset(contentOffset, animated: false)
    
    }
    

    Simply call it with: reload(tableView: self.tableView)

提交回复
热议问题