How to get a UITableview to go to the top of page on reload?

前端 未结 13 1919
一个人的身影
一个人的身影 2021-02-01 02:30

I am trying to get a UITableview to go to the top of the page when I reload the table data when I call the following from

- (void)pickerView:(UIPickerView *)pic         


        
13条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-01 02:39

    After reading all of the answers here and elsewhere, I finally found a solution that works reliably and does not show the scrolling after the data is loaded. My answer is based on this post and my observation that you need a negative offset if you are using content insets:

    func reloadAndScrollToTop() {
        tableView.reloadData()
        tableView.layoutIfNeeded()
        tableView.contentOffset = CGPoint(x: 0, y: -tableView.contentInset.top)
    }
    

提交回复
热议问题