iOS UITableView Scroll to bottom of section

Mostafa A. Khattab picture Mostafa A. Khattab · Oct 2, 2014 · Viewed 22.8k times · Source

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.

Answer

Benetz picture Benetz · Oct 2, 2014

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.