如何在 swift 5 中滚动到 tableview 的底部单元格

问题描述 投票:0回答:2

我创建了一个聊天视图,其中使用了tableview,ChatView的基本功能是显示最新消息(最后一个单元格),但是tableView没有滚动到最后一个索引,它总是卡在中间的某个地方

 func scrollToBottom()  {
    let point = CGPoint(x: 0, y: self.chatTableView.contentSize.height + 200.0)
            self.chatTableView.setContentOffset(point, animated: true)
    }
ios swift uitableview
2个回答
1
投票

斯威夫特5

嗨,您只需在加载表格视图后调用这行代码即可。

  self. chatTableView.scrollToRow(at: IndexPath(row: lastRowIndex, section: 0), at: .bottom, animated: true)

(您必须输入 tableView 最后一行的索引来代替“lastRowIndex”。您可以使用用于填充 tableView 的数组的计数值。例如:(yourArray.count - 1))


0
投票
override func viewDidLoad() {
    super.viewDidLoad()
    
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {
        self.scrollToBottom()
    }
}
private func scrollToBottom(){
    let y = self.collectionView.contentSize.height - self.collectionView.btHeight() + self.collectionView.adjustedContentInset.bottom
    self.collectionView.setContentOffset(CGPoint.init(x: 0, y: y), animated: false)
    if self.loadingView.superview != nil {
        self.loadingView.removeFromSuperview()
    }
    
}
© www.soinside.com 2019 - 2024. All rights reserved.