Pseudocode for detecting bottom of TableView

2019-09-18 10:29发布

问题:

I am trying to detect when the user gets to the bottom of the table in Xamarin.iOS. I have created this pseudocode in order to detect the bottom, however as soon as the application runs it prints that it is already at the bottom of the table when in fact it is not..

float height = tableView.Frame.Size.Height;
float contentYoffset = tableView.ContentOffset.Y;
float distanceFromBottom = tableView.ContentSize.Height - contentYoffset;

if (distanceFromBottom < height) {
    Console.WriteLine ("Bottom of Table");

    }

Any suggestions to improve this code or any better methods of detecting the bottom?

Here is my TableView.cs Class:

public class TableView : UITableView, ITableCellProvider<Datum>
{
    public TableView ()
    {
    }

    public TableView (IntPtr handle) : base(handle)
    {
    }

    public UITableViewCell GetCell (Datum item)
    {
        var newCell = this.DequeueReusableCell(InstagramCell.Key) 
            as InstagramCell ?? InstagramCell.Create();

        newCell.Bind (item);

        return newCell;
    }

    public float GetHeightForRow (NSIndexPath indexPath)
    {
        return 340f;
    }
}

回答1:

Something like this should work:

public override UITableViewCell GetCell (UITableView tableView, NSIndexPath indexPath)
{
  if (indexPath.Section + 1 == NumberOfSections(tableView) && RowsInSection(tableView, indexPath.Section) == indexPath.Row + 1) {
    // this is the last row in the last section
  }
}