Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement UITableView infinite scrolling [duplicate]

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)

like image 836
Conflagrationator Avatar asked Jun 21 '13 21:06

Conflagrationator


2 Answers

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.

like image 122
Quxflux Avatar answered Nov 15 '22 17:11

Quxflux


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.

like image 28
Amro Avatar answered Nov 15 '22 17:11

Amro