How to implement UITableView infinite scrolling [duplicate]
Asked Answered
I

3

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)

Ionia answered 21/6, 2013 at 21:32 Comment(1)
Here is a demo of infinite table view in Swift: github.com/i-schuetz/tableview_infiniteFlaw
S
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.

Sibilant answered 22/6, 2013 at 8:35 Comment(6)
Because INT_MAX freeze the app.Extraterritorial
Probably because of an issue in your implementation. The code has been tested and is working (except for an assertion failure. See the description update)Sibilant
My code haven't issues. I try to return INT_MAX and obviously the app is blockedExtraterritorial
pastebin.com/TergdtQHSibilant
This isn't a solution...is an illusion...if i scroll many times the illusion finish...Extraterritorial
if there a better solution?Oneiric
P
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.

Process answered 22/6, 2013 at 2:53 Comment(0)
S
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 ?

Soporific answered 21/6, 2013 at 21:41 Comment(1)
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.Ionia

© 2022 - 2024 — McMap. All rights reserved.