Mobile

Adding UIRefreshControl to UITableView – Xamarin.iOS

By 25 July 2016 June 8th, 2020 No Comments

Adding UIRefreshControl to UITableView – Xamarin.iOS

How to Add UIRefreshControl to UITableView

Sharing is caring!

Reloading a UITableView in an iOS app is a widely popular Gesture. Adding PullToRefresh to a UITableView in Xamarin.iOS is rather straightforward with the help of UIRefreshControl.

Steps to implement Pull to refresh:

  1. Initialize UIRefreshControl
  2. Add it to the UITableView
  3. Handle the ValueChanged event of UIRefreshControl
  4. Set data in the UITableViewSource and reload the UITableView

Here is the code to implement pull to refresh:

Assumptions:

TableView – reference to the TableView
DataSource – is a class which inherits UITableViewSource
DataSource.Objects – is a public List< object >(), accessible to the UIViewController

        private UIRefreshControl refreshControl;

        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            // Set the DataSource for the TableView
            TableView.Source = dataSource = new DataSource(this);

            // Create the UIRefreshControl
            refreshControl = new UIRefreshControl();

            // Handle the pullDownToRefresh event
            refreshControl.ValueChanged += refreshTable;

            // Add the UIRefreshControl to the TableView
            TableView.AddSubview(refreshControl);
        }

        private void refreshTable(object sender, EventArgs e)
        {
            fetchData();
            refreshControl.EndRefreshing();
            TableView.ReloadData();
        }

        private void fetchData()
        {
            var objects = new List<object>();
            // fetch data and store in objects.
            dataSource.Objects = objects;
        }

References: https://developer.xamarin.com/api/type/UIKit.UIRefreshControl/

Do share whether this works out for you in the comments below.

Leave a Reply

Get started with Bloom