Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MonoTouch - Detect UITableView scrolling

I have a MonoTouch iPhone app which has a UITableViewController as it's main view controller.

I am trying to detect when the tableview is being scrolled with the code:

this.TableView.Scrolled += TableViewScrolled;

where TableViewScrolled(object sender, EventArgs e) { } is my method being called. But it fails to call the TableViewScrolled() for some reason.

Does anyone have any experience with this?

like image 252
Brett Avatar asked Feb 27 '12 01:02

Brett


3 Answers

My answer is not actually only about the scrolling, but because of the fact that I've spent quite an hour fighting with a problem caused by the scrolling event, I've decided to write answer here.

So, the problem for me was that after subscribing to:

this.myTableView.Scrolled += (sender, args) =>
  {
      ...
  };

my overriden methods of inherited from UITableViewDataSource class:

    public override UIView GetViewForHeader(UITableView tableView, int section)
    public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath)

stopped working (became not called). Which was... obviously, pretty unexpected!

So, I just had to override

    public override void Scrolled(UIScrollView scrollView)

in the same datasource delegate instead of subscribing to existent event on table view.

I hope, someone won't get into such trouble!

like image 196
Agat Avatar answered Nov 19 '22 14:11

Agat


@Krumelur's comment on the previous answer makes a very good point and IMO should have been an answer (not a comment).

The point is that if your set a Delegate (or WeakDelegate) to your UITableView then it's events won't work anymore.

Why ? because to implement those events MonoTouch creates it's own internal delegate type for UIScrollView (the parent of UITableView) where it overrides the methods (of the delegate) and expose them as, more natural (in .NET), events inside the type.

This effectively means that you cannot mix those UITableView events with your own delegate UITableViewDelegate type (since the later will override the former).

like image 34
poupou Avatar answered Nov 19 '22 13:11

poupou


Read this documentation: UIScrollViewDelegate

You can use:

- (void) scrollViewDidScroll:(UIScrollView *)scrollView {


}

or

- (void) scrollViewWillBeginDragging:(UIScrollView *)scrollView {


}

Good luck, Nathan

like image 37
Blazer Avatar answered Nov 19 '22 13:11

Blazer