Introduction

In this post you will see how to implement LazyLoading on your TableView. In my previous article, I explained how to manage the scrolling. It’s time to continue our journey with Xamarin IOS!

What is LazyLoading ?

The lazy loading is a design pattern. The idea is to load only what you need. The consequence is a better efficiency of your application. Because you reduce the amount of data and the time needed to load everything. (More information on wikipedia)

TableView LazyLoading

Model & Data

In this example, we will need a model. We will use a very simple one: a comment with only one field.

Now we will add some fake data. Actually, we need enough data to reach the end of out TableView. It’s depends on the size of your cell of course.

LazyLoading Implementation

Obviously we need a method to load our data. In this example, there are two parameters. The first one allows you to define the start index, and the second one the number of items we want to load each time we reach the end of the scrolling. (More information about the GetRange here)

ScrollToEnd Event in UITableViewSource

We need to detect when we arrive at the end of the table. And we also need to override the Scrolled method to accomplish that. The idea is to call the action ScrolledToEnd when we are at the end of the scroll.

RefreshData

The following code implements the method we just wrote in the TableView, the famous action scrolledToEnd. And it loads our data. So we can call this function in our ViewDidAppear. The _commentsBuffer will be the source of the TableView. For more information read the code comments bellow.

(More information about ScrollToRow here)

Conclusion

With this small piece of code, you should be able to deferred the load of the data. Furthermore, now your are able to implement LazyLoading in a TableView.

Happy coding ! 🙂