2

How can I scroll to last row in tableview from current positon, because I have tried this solution:

self.tableView.scrollToRow(at: lastIndex.last!, at: .bottom , animated: false)


let numberOfSections = self.tableView.numberOfSections
            let numberOfRows = self.tableView.numberOfRows(inSection: numberOfSections-1)

        if numberOfRows > 0 {
            let indexPath = IndexPath(row: numberOfRows-1, section: (numberOfSections-1))
            self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: animated)
        }

and it doesn't scroll from the current position but from the most top position. Even though my current position is the last row

Abizern
  • 146,289
  • 39
  • 203
  • 257
Benny Wijaya
  • 207
  • 3
  • 16

4 Answers4

4

Do

func scrollToLastRow() {
  let indexPath = NSIndexPath(forRow: objects.count - 1, inSection: 0)
  self.tableView.scrollToRowAtIndexPath(indexPath, atScrollPosition: .Bottom, animated: true)
}

Scrolling means, thats from current position. So this code should work.

Otherwise you can specify the frame to scroll using scrollRectToVisible

tableView.scrollRectToVisible(tableView.rectForRowAtIndexPath(indexPath),animated: true) 
Saranjith
  • 11,242
  • 5
  • 69
  • 122
0

Try this code,

yourTableView.scrollRectToVisible(CGRect.init(x: 0, y: yourTableView.contentSize.height - yourTableView.frame.size.height, width: yourTableView.frame.size.width, height: yourTableView.frame.size.height), animated: true)
Brijesh Shiroya
  • 3,323
  • 1
  • 13
  • 20
0

You have to provide some more info that when you want to scroll your tableview to last index,Because this code works perfectly

let indexPath = IndexPath.init(row: lastRow, section: 0)
customTableView.scrollToRow(at: indexPath, at: .bottom, animated: true)

Updated

Finally here is working code for me

  @IBAction func sendBtnPressed(_ sender: UIButton) {
    self.listMessages.append(messageTextView.text)
    let lastIndexPath = IndexPath.init(row: self.listMessages.count-1, section: 0)
    self.tableView.insertRows(at: [lastIndexPath], with: .none)
    self.tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true)
    messageTextView.text = "";
    }
Vikky
  • 914
  • 1
  • 6
  • 16
  • i've tried your code, but it still same. when i check it in cellforrowat,it keeps scroll entire table eventhough i am in my last row. i checked it by print the row in cellforrowat, it prints from 0 to last row, for each scroll, even though the animation doesn't show it scroll from top. – Benny Wijaya Jul 20 '17 at 05:54
  • can you tell me the position where you are writing this code – Vikky Jul 20 '17 at 06:15
  • i add it in sendMessage function. so for each time when user send message it will reload the table and scroll to last row – Benny Wijaya Jul 20 '17 at 08:25
  • Have you added this code after reloading your tableView.If not please do it – Vikky Jul 20 '17 at 08:28
  • i have do it before //to reload the last row let lastIndex = [IndexPath(row: listMessages.count-1, section: 0)]; self.tableView.beginUpdates() self.tableView.insertRows(at: lastIndex, with: .none) self.tableView.endUpdates() let indexPath = IndexPath.init(row: self.listMessages.count-1, section: 0) self.tableView.scrollToRow(at: indexPath, at: .bottom, animated: true) – Benny Wijaya Jul 20 '17 at 08:32
  • Try replace your code by this to make sure what problem is @IBAction func sendMessage(_ sender: Any) { if textBox.text?.isEmpty == false { self.listMessages.append(textBox.text) self.tableView.reloadData let lastIndexPath = IndexPath.init(row: self.listMessages.count-1, section: 0) self.tableView.scrollToRow(at: lastIndexPath, at: .bottom, animated: true) textBox.text = ""; } } – Vikky Jul 20 '17 at 08:42
  • still same T_T , if i add self.tableView.reloadData. it will getting worse which means i have to reload entire row for each send message. and i've changed it to just reload the new cell (new message). but the problem for now is the scrolling. it scroll entire table. and maybe that's how it works? for scrolling i mean – Benny Wijaya Jul 20 '17 at 08:51
  • Yes you are correct that is is needless to reload full table but i was trying to get actual cause why this is happening,It would be better if you can provide me your full class , through GitHub or by some other process if you can – Vikky Jul 20 '17 at 09:02
  • i found the bug it's caused by this . tableView.estimatedRowHeight = self.view.frame.height * 0.3 . because i want to set the row height is dynamical, when i remove this your code works. but i need the row height to be dynamic – Benny Wijaya Jul 21 '17 at 04:13
  • OK if you want dynamic row height then add these two lines in viewDidLoad -------yourTableView.estimatedRowHeight = yourCellHeightInStoryBoard and yourTableView.rowHeight = UITableViewAutomaticDimension – Vikky Jul 21 '17 at 04:19
  • about my cell height, how can i get it? it is constant value ? which i set it manually? – Benny Wijaya Jul 21 '17 at 04:24
  • Now i got it you have constraints problem in your layout , so refer to this link https://www.appcoda.com/self-sizing-cells/ – Vikky Jul 21 '17 at 04:30
  • https://stackoverflow.com/questions/25686490/ios-8-auto-cell-height-cant-scroll-to-last-row, it's a bug from apple – Benny Wijaya Jul 21 '17 at 05:12
  • So finally you got the issue :) and I was using iOS 9 and solution is also provided in the link provided by you.Hope you have not missed that – Vikky Jul 21 '17 at 05:20
  • i have found the solution, check my updated question and btw thanks for giving your time to help me. god bless you – Benny Wijaya Jul 21 '17 at 06:47
  • Glad to hear you found the solution and thanks for your appreciation :) – Vikky Jul 21 '17 at 06:57
0

For swift 4

func scrollToLastRow() {

    if CommentNameArray.count != 0 {
        let indexPath = NSIndexPath(row: CommentNameArray.count - 1, section: 0)
        self.CommentTableView.scrollToRow(at: indexPath as IndexPath, at: .bottom, animated: true)
    }

}
Abdul Saboor
  • 102
  • 5