Ios UITableview仅滚动到中间-Swift 4

Ios UITableview仅滚动到中间-Swift 4,ios,uitableview,scroll,swift4,Ios,Uitableview,Scroll,Swift4,我有一个包含聊天信息的表视图。我试图滚动到tableview的底部,但它只滚动了一半。我怀疑这与单元格的高度不同有关(因为有些消息比其他消息长) 是否有一种方法可以完全滚动到底部,而不是基于平均单元格高度 下面是我用来滚动到底部的代码: let lastIndex = IndexPath(row: messages.count - 1, section: 0) self.tableview.scrollToRow(at: lastIndex, at: .bottom, animated: tru

我有一个包含聊天信息的表视图。我试图滚动到tableview的底部,但它只滚动了一半。我怀疑这与单元格的高度不同有关(因为有些消息比其他消息长)

是否有一种方法可以完全滚动到底部,而不是基于平均单元格高度

下面是我用来滚动到底部的代码:

let lastIndex = IndexPath(row: messages.count - 1, section: 0)
self.tableview.scrollToRow(at: lastIndex, at: .bottom, animated: true)

使用TableView扩展

import UIKit

    extension UITableView {
        func scrollToBottom(animated: Bool) {
            let y = contentSize.height - frame.size.height
            setContentOffset(CGPoint(x: 0, y: (y<0) ? 0 : y), animated: animated)
        }
    }
tableView.scrollToBottom(animated: true)