views:

81

answers:

3

I have a set of 100 rows, pretty similar to values which can be selected in a picker. When the user scrolls the table, I want the rows to be appended like an forever-ongoing assembly-belt. So when the user scrolls down and reaches the row 100, and scrolls even further, the table view will show again row 1, and so on. Reverse direction same thing.

My thoughts:

  • don't display scroll indicators (they would make not much sense, probably)
  • what value to return in the numberOfRows delegate method? This infinity constant?
  • in cellForRowAtIndexPath: simply wrap the index around when it exceeds bounds?
+2  A: 

I haven't tried this but here's an idea. Let's say N = 100, since you have 100 rows. What we'll do is tell the tableview we have 200 rows, but we'll keep the user scrolling in the range 50-150.

Tell (the tableview) that you have double that number of rows, so for numberOfRows, return 2*N. In cellForRowAtIndexPath, always return the cell corresponding to (row % N).

Now init the table scrolled to row N (instead of 0) so that the user can scroll in either direction.

As the user scrolls up the tableview will eventually ask for row N+(N/2), when that happens send a scrollToRowAtIndexPath:atScrollPositionanimated: to scroll to the row-N. That will reposition the table back by N (ie: from 150 to 50), same cell but we'll never run off the end of the table this way.

Do the same when the tableview asks for cell at row N-(N/2) for scrolling up.

You'll have to deal with the special case where N cells fits in the view with extra room, ie: what if N were 1?

progrmr
+3  A: 

Many apps do this. The idea is that the beginning of the table is always the same, but you keep adding to the end, so the table just keeps growing.

Let's say you first have 100 data elements. Your numberOfRows returns 101 then. First 100 cells are normal. And scroll indicators still make sense.

If the 101st cell is displayed, you display a progress indicator like UIActivityIndicator in the cell, and initiate the process to load next 100 rows. When the data arrives, you either reload the whole table with UITableView reloadData, or you insert new cells individually with UITableView insertRowsAtIndexPaths:withRowAnimation:.

So, you just keep infinitely appending to the table. This is easier than trying to keep the table as a "window" that is always at N cells/rows and unload from the beginning of the table. If you need this sort of window, you may reconsider if this is really the best way to interact with your data.

Jaanus
+3  A: 

I just finished implementing a variation of this.

Lets assume you have a 100 rows and 8 of them fit in a screen. Start by adding the last 8 rows first, then the 100 rows normally and then the first 8 rows at the end. Recenter the tableview by using setContentOffset to the first row of the 100.

When the user scrolls below the 100th one he will see rows 1-8, you can detect this in the viewDidScroll delegate function and recenter the scroll back to the first record (ensure u set the animated flag to NO when you do this). Similarly when the user scrolls past 1, he/she will see the last 8 records, you can again recenter the scroll view to show the last 8 records in the 100 row section.

This actually implements circular scrolling and the user can keep scrolling in both directions.

Bharat Ahluwalia