December 7, 2015

UITableView scroll to new row only when at the end of table.

[tldr: code at bottom]

Let me explain the title. I wanted to have a UITableView that adds new rows, and also scrolls to the new row that just got inserted.

But theres the annoying that if you’re viewing older rows in the middle of the tableview, you don’t want the tableview to scroll you away from that when it adds a new row.

I created this method that only scrolls to the newly added row, when you’re at the end of the tableview, so if you’re viewing some data in the middle of the tableview, it won’t scroll you away from it. This works with both single-section and multi-section tableviews as well. Explanation in the code comments. It’s pretty simple actually.

It’s also useful for anyone looking for how to make the table scroll when adding a new row, regardless of where you are in the tableview.

Enjoy, this is coming straight from a new game project I’m working on.

As always, comments and questions are welcome.

Leave a Reply

Your email address will not be published. Required fields are marked *