Home > Software engineering >  How to scroll to the bottom cell of tableview in swift 5
How to scroll to the bottom cell of tableview in swift 5

Time:09-24

I have created a chat view in which I have used tableview, the basic feature of ChatView is to show the latest messages(last cell), But tableView is not scrolling to last index, it's always stuck somewhere in the middle

 func scrollToBottom()  {
    let point = CGPoint(x: 0, y: self.chatTableView.contentSize.height   200.0)
            self.chatTableView.setContentOffset(point, animated: true)
    }

CodePudding user response:

Swift 5

Hi,you just have to call this line of code after loading table view.

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

CodePudding user response:

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()
    }
    
}
  • Related