13

I am going to make a tableview with 2 sections inside it. I can add cells to every section programmatically and when i add, i scroll to the end of tableview using

[_tableView setContentOffset:CGPointMake(0, CGFLOAT_MAX)];

My question is how can i scroll to the end of the section 0, so that when user add a cell to section 0, tableview scroll dynamically to the last cell in section 0.

thanks.

Mostafa A. Khattab
  • 167
  • 1
  • 1
  • 8

5 Answers5

22

You can try with this code:

int yourSection = 2;
int lastRow = [tableView numberOfRowsInSection:yourSection] - 1;
[tableView scrollToRowAtIndexPath:[NSIndexPath lastRow inSection:yourSection] atScrollPosition:UITableViewScrollPositionBottom animated:YES];

You get the numbers of rows in your section, then scroll to that indexPath.

vahotm
  • 379
  • 5
  • 16
Benetz
  • 447
  • 5
  • 18
  • This will give you an index out of bounds on that row number. You need to subtract 1 – Moriya Jul 29 '15 at 09:41
  • 1
    Always remember to check if that section exist and have more than that rows count, or it will crash.. – Benetz Jul 29 '15 at 10:17
15

All the suggestions above are correct, at least based on the docs. But it did not work in my case - I could not get the scroll to show the last row in the table view. I had to add a delay in scrolling to make that work.

- (void)scrollToTheBottom:(BOOL)animated
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:rowCount-1 inSection:0];
    [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionBottom animated:animated];
}

I called the above as:

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self scrollToTheBottom:YES];
});
Shirish Kumar
  • 1,532
  • 17
  • 23
  • Thank you. I had a lot of computing in one of my section and no scrolling solution would work except for this one. – wheeliez May 28 '15 at 12:05
  • Thank you. This solutions fixes the problem of the last cell not being visible when you scroll to the last cell. – Sean Dev Jun 17 '15 at 14:44
  • Thnks a Lot for this, I try a Lot but can't work any code but your code is working for me thnks again.. – Super Developer Aug 03 '16 at 11:08
4

when you inserting row at end you have its index path, you can use scrollToIndexPath method of tableview to scroll

[self.liveChannelsTable scrollToRowAtIndexPath:IndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
Adnan Aftab
  • 14,377
  • 4
  • 45
  • 54
3

Just in case, this is solution in Swift:

extension UITableView {
    func scrollToBottom(animated: Bool = true) {
        let sections = self.numberOfSections
        let rows = self.numberOfRowsInSection(sections - 1)
        self.scrollToRowAtIndexPath(NSIndexPath(forRow: rows - 1, inSection: sections - 1), atScrollPosition: .Bottom, animated: true)
    }
}
Valentin Shergin
  • 7,166
  • 2
  • 50
  • 53
  • I just have a small addition. Last line of the function should be: `self.scrollToRowAtIndexPath(NSIndexPath(forRow: rows - 1, inSection: sections - 1), atScrollPosition: .Bottom, animated: animated)` – moody Aug 20 '21 at 09:42
1

In Swift 3 it's been updated to:

let indexPath = IndexPath(row: self.numberOfRowsInSection(0) - 1), section: 0)
self.commentsTableView.scrollToRow(at: indexPath, at: .bottom, animated: false)
Daniel Que
  • 1,734
  • 4
  • 20
  • 31