9

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)

Conflagrationator
  • 251
  • 1
  • 3
  • 8
  • Here is a demo of infinite table view in Swift: https://github.com/i-schuetz/tableview_infinite – Ixx Oct 30 '15 at 16:37

3 Answers3

7

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.

TheNiftyCoder
  • 2,687
  • 2
  • 22
  • 40
2

UITableView is a UIScrollView subclass. Implement - (void)scrollViewDidScroll:(UIScrollView *)scrollView (part of the UIScrollViewDelegate protocol) and use the contentOffset property. For example, if the contentOffset.y is close to zero, you know you're near the top of the table and you can insert cells above your current position.

Amro
  • 4,503
  • 1
  • 14
  • 9
1

From a previous post: UITableView infinite scrolling

essentially you don't want to generate the additional data until the user goes looking for it, you can hook into the cellForRowAtIndexPath: method to know when they are approaching the end of your list and then extend it at that point.

If you are using it for dates why don't you just use a picker view ?

Community
  • 1
  • 1
Christophe
  • 46
  • 3
  • I've tried the looking for the end way, but it only works if you are going one way, and how would I tell the view the number of rows if the user is scrolling backwards? Also, I am doing some custom drawing for each date, so I need a cell for each date, but different things will be on each. depending on the data of course. – Conflagrationator Jun 22 '13 at 01:24