<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Gus Eulalio</title>
    <description>The latest articles on DEV Community by Gus Eulalio (@guseulalio).</description>
    <link>https://dev.to/guseulalio</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F287539%2F0060816c-1332-4ccc-8d07-617ecac74597.png</url>
      <title>DEV Community: Gus Eulalio</title>
      <link>https://dev.to/guseulalio</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/guseulalio"/>
    <language>en</language>
    <item>
      <title>Building a pseudo-infinite scroller</title>
      <dc:creator>Gus Eulalio</dc:creator>
      <pubDate>Wed, 23 Aug 2023 10:36:11 +0000</pubDate>
      <link>https://dev.to/guseulalio/building-a-pseudo-infinite-scroller-2kj4</link>
      <guid>https://dev.to/guseulalio/building-a-pseudo-infinite-scroller-2kj4</guid>
      <description>&lt;p&gt;So you were given the task of building an infinite table view or carousel, and they want it for yesterday. Now you're scratching your head wondering about the inner workings of a table view.&lt;/p&gt;

&lt;p&gt;Fret not. There's a quick solution for this, or a temporary workaround while you don't build the real thing.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we'll do
&lt;/h2&gt;

&lt;p&gt;We'll make a table view 10,000 cells long, point the viewing window to the middle of the collection, and cycle through the data source.&lt;/p&gt;

&lt;h2&gt;
  
  
  Before we start
&lt;/h2&gt;

&lt;p&gt;I won't build anything from scratch here. I'll consider you have a working table view (or collection view) that you want to convert into an infinite scroller.&lt;/p&gt;

&lt;p&gt;I'll do this based on a table view, all changes are made on the table view controller / data source. It isn't much different for using a collection view.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to
&lt;/h2&gt;

&lt;p&gt;First up, go to your &lt;code&gt;tableView(tableView:, cellForRowAt:)&lt;/code&gt; method, and replace the instances of &lt;code&gt;indexPath.row&lt;/code&gt;, with &lt;code&gt;indexPath.row % datasource.count&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This will make sure you keep getting data when the table view asks for an element outside the datasource range.&lt;/p&gt;

&lt;p&gt;Next, create the constant below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;numberOfCells&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10_000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will be the size of your table view. The user can obviously reach the end of the table view, but it will take them scrolling for a long time in one direction, which is an unlikely behaviour.&lt;/p&gt;

&lt;p&gt;Then replace &lt;code&gt;tableView(tableView: numberOfRowsInSection:)&lt;/code&gt; with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt;
&lt;span class="kd"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;tableView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="nv"&gt;tableView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;UITableView&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
               &lt;span class="n"&gt;numberOfRowsInSection&lt;/span&gt; &lt;span class="nv"&gt;section&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;numberOfCells&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, add the following line to &lt;code&gt;viewDidLoad()&lt;/code&gt; to avoid revealing our secret:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="n"&gt;tableView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;showsVerticalScrollIndicator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will hide the scroll indicator, which would indicate to the user that they're not really seeing an infinite table view.&lt;/p&gt;

&lt;p&gt;And to finish up, add the below to &lt;code&gt;viewDidAppear(:)&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="nv"&gt;indexPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;IndexPath&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;row&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;numberOfCells&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;section&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;tableView&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;scrollToRow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;indexPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                      &lt;span class="nv"&gt;at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;top&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;animated&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This last part will point the scroll view to the middle of the table.&lt;/p&gt;

&lt;p&gt;There you go, now you can run it and see it working yourself.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limitations
&lt;/h2&gt;

&lt;p&gt;Again, this isn't a truly infinite table view. Even though it's hard to reach the ends of the table in any real-life scenario, this is still a possible scenario, especially if you need to programmatically manipulate the scroll position for some reason.&lt;/p&gt;

&lt;p&gt;Also, you may think 10,000 is a small number and want some extra leeway. That's fine, but don't go too crazy, there is an increase in the memory footprint, and it may get slow quickly especially in old hardware.&lt;/p&gt;

&lt;p&gt;Hope you find this useful.&lt;/p&gt;

</description>
      <category>swift</category>
      <category>ios</category>
    </item>
  </channel>
</rss>
