Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 402
Skwiggs Avatar asked Oct 06 '17 06:10

Skwiggs


1 Answers

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)
    }
}
like image 170
Skwiggs Avatar answered Nov 05 '22 17:11

Skwiggs