Note that there are some explanatory texts on larger screens.

plurals
  1. POEternal scrolling UITableView
    primarykey
    data
    text
    <p>I'm trying to create a UITableView with dates, shouldn't be very exciting, I know. It starts around the current date, but the user should be able to scroll down (the future) <em>and up (the past)</em> as far as he/she wants. This results in a potentially infinite number of rows. So what's the way to go about creating this?</p> <p>Returning <code>NSIntegerMax</code> as the number of rows already crashes the app, but even if it wouldn't, that still doesn't account for being able to scroll <em>up</em>. I could start half way of course, but eventually there's a maximum. </p> <p>Any ideas how to do or fake this? Can I update/reload the table somehow without the user noticing, so I never run into a border?</p> <p><strong>SOLUTION:</strong><br> I went with @ender's suggestion and made a table with a fixed amount of cells. But instead of reloading it when the user scrolls to near the edges of the fixed cells, I went with reloading the table when the scrolling grinds to a halt. To accomodate with a user scrolling great distances without stopping, I just increased the row count to 1000, putting the ROW_CENTER constant to 500. This is the method that takes care of updating the rows.</p> <pre><code>- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView { NSArray *visible = [self.tableView indexPathsForVisibleRows]; NSIndexPath *upper = [visible objectAtIndex:0]; NSIndexPath *lower = [visible lastObject]; // adjust the table to compensate for the up- or downward scrolling NSInteger upperDiff = ROW_CENTER - upper.row; NSInteger lowerDiff = lower.row - ROW_CENTER; // the greater difference marks the direction we need to compensate NSInteger magnitude = (lowerDiff &gt; upperDiff) ? lowerDiff : -upperDiff; self.offset += magnitude; CGFloat height = [self tableView:self.tableView heightForRowAtIndexPath:lower]; CGPoint current = self.tableView.contentOffset; current.y -= magnitude * height; [self.tableView setContentOffset:current animated:NO]; NSIndexPath *selection = [self.tableView indexPathForSelectedRow]; [self.tableView reloadData]; if (selection) { // reselect a prior selected cell after the reload. selection = [NSIndexPath indexPathForRow:selection.row - magnitude inSection:selection.section]; [self.tableView selectRowAtIndexPath:selection animated:NO scrollPosition:UITableViewScrollPositionNone]; } } </code></pre> <p>The magic breaks when a user scrolls to the edge of the table without stopping, but with the table view <code>bounces</code> property disabled, this merely feels like a minor glitch, yet totally acceptable. As always, thanks StackOverflow!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload