Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS UITableView Scroll to bottom of section

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.

like image 415
Mostafa A. Khattab Avatar asked Oct 02 '14 12:10

Mostafa A. Khattab


People also ask

How do I scroll to the bottom of Uitableview?

To scroll to the bottom of the table view items, use ScrollToRow. Three parameters are required for this: NSIndexPath: Specify which row item to scroll to.


1 Answers

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];
});
like image 65
Shirish Kumar Avatar answered Sep 17 '22 00:09

Shirish Kumar