uitableviewxamarinxamarin.iostable-footer

Add a Custom Footer at the end of UITableView in Xamarin.iOS


I want to add a footer view to a UITableView that shows a UIProgresIndicator when the user has reached the end of the list and new data will be loaded, or a UILabel when there are no more data to be fetched.

I have used the code below, but nothing happens:

UITableViewHeaderFooterView footer = new UITableViewHeaderFooterView ();

UILabel futerlabel = new UILabel ();
futerlabel.Text = "Duke u ngarkuar";

footer.AddSubview (futerlabel);

Any way how to achieve this.


Solution

  • If you are in a UITableViewController try this:

            TableView.TableFooterView = footer;
    

    UPDATE

    After having a think about this I would suggest not using a footer but rather an extra cell at the end of your data, this will get this effect.

    Using this method to check if you have scrolled to the last item and to update the table's data:

    public override void WillDisplay (UITableView tableView, UITableViewCell cell, NSIndexPath indexPath)
        {
            // if showing last row of last section, load more
            if (indexPath.Section == tableView.NumberOfSections()-1 && indexPath.Row == tableView.DataSource.RowsInSection(tableView, indexPath.Section)-1 && !FullyLoaded) {
                var growRowSource = tableView.DataSource as GrowRowTableDataSource;
                if (growRowSource != null) {
                    FullyLoaded = growRowSource.LoadNextPage ();
                    Task.Delay (5000);
                    tableView.ReloadData ();    
                }
    
            }
        }
    

    And then in the Delegate checking for the last item and creating a different cell like so:

    public override UITableViewCell GetCell (UITableView tableView, Foundation.NSIndexPath indexPath)
        {
            if (indexPath.Row == LoadedItems.Count) {
                var loadingCell = tableView.DequeueReusableCell (LoadingCellID, indexPath) as LoadFooterCell;
                loadingCell.Loading = !hasNextPage;
                return loadingCell;
            }
            var cell = tableView.DequeueReusableCell (CellID, indexPath) as GrowRowTableCell;
            var item = LoadedItems [indexPath.Row];
    
            // Setup
            cell.Image = UIImage.FromFile(item.ImageName);
            cell.Title = item.Title;
            cell.Description = item.Description;
    
            return cell;
        }
        bool hasNextPage;
    

    I quickly mocked an example from the GrowTable Xamarin sample code here: https://github.com/b099l3/LoadingTableExample