How to implement UITableView infinite scrolling

Conflagrationator picture Conflagrationator · Jun 21, 2013 · Viewed 11.3k times · Source

I need to make a dynamic UITableView that supports Infinite Scrolling in both directions. I need to make it to where it doesn't loop, rather it just keeps going in the direction since it will be dealing with dates.

The tableView is going to be in a UIViewController amongst two other UITableViews; one of them static, and the other dynamic (in the default way). this also raises the question of what to do with some of my Datasource methods. Namely tableView:NumberOfRowsInSection since the number of data points I have will be algorithmically generated and therefore possibly infinite (as in: there is not a defined quantity to the data until it reveals all of it)

Answer

TheBeardedCoda picture TheBeardedCoda · Jun 22, 2013

You should return INT_MAX in tableView:NumberOfRowsInSection and then trick a little bit in the tableView:cellForRowAtIndexPath:

Let's say you want the days of week infinitely. So you would have an array of @[@"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", @"Sunday"]. Then the value for your current row is indexPath.row % array.count.That way you can map a small amount of data to a nearly endless number of rows.

To make the infinite illusion work you should select a row near INT_MAX/2.

Update:

The tableview might trigger an exception when returning a too big number of rows. In that case specify the number of rows yourself. 100.000 rows are working fine.