How to keep UITableView contentoffset after calling -reloadData

前端 未结 13 1305
隐瞒了意图╮
隐瞒了意图╮ 2020-11-28 02:43
CGPoint offset = [_table contentOffset];
[_table reloadData];
[_table setContentOffset:offset animated:NO];    //unuseful

//    __block UITableView *tableBlock = _t         


        
相关标签:
13条回答
  • 2020-11-28 03:22

    If you insert data at the beginning of your dataSource array, you need to change contentOffset like this: Swift 3+

    func prepareReloadData() {
        let previousContentHeight = tableView.contentSize.height
        let previousContentOffset = tableView.contentOffset.y
        tableView.reloadData()
        let currentContentOffset = tableView.contentSize.height - previousContentHeight + previousContentOffset
        tableView.contentOffset = CGPoint(x: 0, y: currentContentOffset)
    }
    
    0 讨论(0)
提交回复
热议问题