New to Telerik UI for Xamarin? Download free 30-day trial

ListView for Xamarin.iOS: Load-on-demand

There are certain scenarios typically with remote data over the wire where data needs to be loaded continuously on small portions. TKListView can load data on demand.

Enabling the load-on-demand

To enable the load on demand feature, you shoud set the LoadOnDemandMode property to one of the two supported modes Auto or Manual.

listView.LoadOnDemandMode = TKListViewLoadOnDemandMode.Manual;

When using the Auto LoadOnDemand mode the LoadOnDemandBufferSize of type int property defines the number of cells from the bottom of the list view up at which to start requesting data.

listView.LoadOnDemandBufferSize = 5;

After setting the desired LoadOnDemandMode you should implement the TKListViewDelgate method ShouldLoadMoreDataAtIndexPath: to determine if more data should be loaded. After the data is loaded you should notify the ListView by calling its DidLoadDataOnDemand method:

class ListViewDelegate: TKListViewDelegate
{
    ListViewLoadOnDemand owner;

    public ListViewDelegate(ListViewLoadOnDemand owner)
    {
        this.owner = owner;
    }

    public override bool ShouldLoadMoreDataAtIndexPath (TKListView listView, NSIndexPath indexPath)
    {
        DispatchQueue.DefaultGlobalQueue.DispatchAsync (() => {
            this.owner.lastRetrievedDataIndex = Math.Min(this.owner.names.Items.Length, this.owner.lastRetrievedDataIndex + 10);
            DispatchQueue.MainQueue.DispatchAfter(new DispatchTime(DispatchTime.Now, 2 * 400000000), new Action(delegate {
                if (this.owner.names.Items.Length == this.owner.lastRetrievedDataIndex) {
                    listView.LoadOnDemandMode = TKListViewLoadOnDemandMode.None;
                }
                listView.DidLoadDataOnDemand();                
            }));
        });

        return true;
    }
}

When using Manual LoadOnDemand mode, TKListView appends a special cell at the end of the list. Touching this cell starts the process of loading more data. In this scenario you should process CellForItem method of TKListViewDataSource and check whether this is a "load on demand cell":

public override TKListViewCell CellForItem (TKListView listView, NSIndexPath indexPath)
{
    TKListViewCell cell = listView.DequeueLoadOnDemandCell (indexPath);

    if (cell == null) {
        cell = listView.DequeueReusableCell ("cell", indexPath) as TKListViewCell;
        cell.ImageView.Image = UIImage.FromBundle (this.owner.photos.Items [indexPath.Row] as NSString);
        cell.TextLabel.Text = this.owner.names.Items [indexPath.Row] as NSString;
        Random r = new Random ();
        cell.DetailTextLabel.Text = this.owner.loremIpsum.RandomString (10 + r.Next (0, 16), indexPath);
        cell.DetailTextLabel.TextColor = UIColor.White;
    }

    cell.BackgroundView.BackgroundColor = UIColor.FromWhiteAlpha (0.3f, 0.5f);
    ((TKView)cell.BackgroundView).Stroke = null;

    return cell;
}

ListView Load On Demand example can be found in our Native Xamarin.iOS examples.

In this article