Pull to refresh functionality
Pull to refresh is becoming more than a requirement in mobile applications. The purpose is to provide the user with the facility of pulling it down to refresh the contents of the table or collection view. In iOS applications, it is has become simplified to add the pull to refresh functionality to tableview or collectionview.
Adding a Refresh Control
Apple provides us the UIRefreshControl class, which simplifies adding the pull to refresh. The First thing we need to do is instantiate the UIRefreshControl class.
Here, we need to add this refresh control to our tableview. We can assign the refreshControl property of tableview to this instance. However, we must notice that, before iOS 10, there was not a property like refreshControl. We had to add it as a sub view to the tableview.
Now, we want our application to perform some action on the table view refresh. We can add it as the target to the UIRefreshControl instance. The syntax is given below.
Now, we are all set up to have a pull to refresh functionality in our application. Consider the following example where we add the pull to refresh to a tableview.
In this simple example, we will create a tableview and populate it with an array. On refreshing, we will append some values to that array and reload the tableview with the array.
Main.storyboardHere, we have created a tableview and add a prototype cell in it. We have added a label inside the cell to display the message. The tableview content will be refreshed on pulling it down.