How do I run code when the user scrolls all the way down a table view?

I need to fetch data from CloudKit to add to the data source of a table view when the user scrolls all the way down to the bottom of the table view. How do I get the table view to trigger code to fetch the data from CloudKit? Is there a notification or an event that fires at that time?

Answered by Claude31 in 342651022

I understand your goal is not to have to load all data first, but load on the fly ?


You can load batches of data, as described here

https://stackoverflow.com/questions/20269474/uitableview-load-more-when-scrolling-to-bottom-like-facebook-application


You can also use prefetching


Prefetching Data

If your table view relies on an expensive data loading process, you can improve your user experience by prefetching data before it is needed for display. Assign an object that conforms to the

UITableViewDataSourcePrefetching
protocol to the
prefetchDataSource
property to receive notifications of when to prefetch data for cells.
var prefetchDataSource: UITableViewDataSourcePrefetching?

The object that acts as the prefetching data source for the table view, receiving notifications of upcoming cell data requirements.


For a complete tutorial:

h ttps://andreygordeev.com/2017/02/20/uitableview-prefetching/


Other reading

h ttps://medium.com/capital-one-tech/boost-smooth-scrolling-with-ios-10-pre-fetching-api-818c25cd9c5d

Accepted Answer

I understand your goal is not to have to load all data first, but load on the fly ?


You can load batches of data, as described here

https://stackoverflow.com/questions/20269474/uitableview-load-more-when-scrolling-to-bottom-like-facebook-application


You can also use prefetching


Prefetching Data

If your table view relies on an expensive data loading process, you can improve your user experience by prefetching data before it is needed for display. Assign an object that conforms to the

UITableViewDataSourcePrefetching
protocol to the
prefetchDataSource
property to receive notifications of when to prefetch data for cells.
var prefetchDataSource: UITableViewDataSourcePrefetching?

The object that acts as the prefetching data source for the table view, receiving notifications of upcoming cell data requirements.


For a complete tutorial:

h ttps://andreygordeev.com/2017/02/20/uitableview-prefetching/


Other reading

h ttps://medium.com/capital-one-tech/boost-smooth-scrolling-with-ios-10-pre-fetching-api-818c25cd9c5d

How do I run code when the user scrolls all the way down a table view?
 
 
Q