简体   繁体   中英

UITableView add rows and scroll to bottom

I'm writing a table view where rows are added upon user interaction. The general behavior is simply to add a single row, then scroll to the end of the table.

This was working perfectly fine before iOS11, but now the scrolling always jumps from the top of the table instead of smoothly scrolling.

Here's the code that has to do with adding new rows:

func updateLastRow() {
    DispatchQueue.main.async {
        let lastIndexPath = IndexPath(row: self.currentSteps.count - 1, section: 0)

        self.tableView.beginUpdates()
        self.tableView.insertRows(at: [lastIndexPath], with: .none)
        self.adjustInsets()
        self.tableView.endUpdates()

        self.tableView.scrollToRow(at: lastIndexPath,
                                   at: UITableViewScrollPosition.none,
                                   animated: true)
    }
}

And

func adjustInsets() {

    let tableHeight = self.tableView.frame.height + 20
    let table40pcHeight = tableHeight / 100 * 40

    let bottomInset = tableHeight - table40pcHeight - self.loadedCells.last!.frame.height
    let topInset = table40pcHeight

    self.tableView.contentInset = UIEdgeInsetsMake(topInset, 0, bottomInset, 0)
}

I'm confident the error lies within the fact that multiple UI updates are pushed at the same time (adding row and recalculating edge insets), and tried chaining these functions with separate CATransaction objects, but that completely messes up asynchronous completion blocks defined elsewhere in the code which update some of the cell's UI elements.

So any help would be appreciated :)

I managed to fix the issue by simply just calling self.tableView.layoutIfNeeded() before adjusting the insets:

func updateLastRow() {
    DispatchQueue.main.async {
        let lastIndexPath = IndexPath(row: self.currentSteps.count - 1, section: 0)

        self.tableView.beginUpdates()
        self.tableView.insertRows(at: [lastIndexPath], with: .none)
        self.tableView.endUpdates()

        self.tableView.layoutIfNeeded()
        self.adjustInsets()

        self.tableView.scrollToRow(at: lastIndexPath,
                                   at: UITableViewScrollPosition.bottom,
                                   animated: true)
    }
}

Make sure that you are respecting the Safe Areas. For more details, check: https://developer.apple.com/ios/update-apps-for-iphone-x/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM