iOS UITableViewController - Insert and Scroll to bottom - sometimes jumping

Viewed 2126

I'm developing an iOS Chat App and I have a problem on the chat view. I'm using UITableViewController for the chat view. Sometimes my table jumps when new row is inserted as you can see in video: https://youtu.be/8IgEUJ5uYAc .

This is how I'm inserting and scrolling to the bottom of the table:

        self.conversation.append(message)

        self.tableView.beginUpdates()
        self.tableView.insertRows(at: [IndexPath(row: self.conversation.count - 1, section: 0)], with: UITableViewRowAnimation.none)
        self.tableView.endUpdates()


        DispatchQueue.main.async {
            self.tableView.scrollToRow(at: IndexPath(row: self.conversation.count - 1, section: 0), at: UITableViewScrollPosition.bottom, animated: false)
            self.footerView?.isHidden = true
            self.theMessage.text = nil
            self.switchBottomActions(showSend: false)
        }

Each message object has a property called estimatedMessageHeight. I'm saving there the message cell size, so tableView's heightForRowAt code is:

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {

    guard let cellHeight = self.conversation[indexPath.row].estimatedHeight else {

        return 0

    }

    return cellHeight

}

Any solution?

3 Answers
Related