Forum Xamarin.iOS

TableView delegates not working

ChrisApersChrisApers USMember


I'm working on my first iOS view and already got a problem! :)

I defined a simple UIViewController having a UIButton on top and a UITableView under this button.

My controller is defined like this:
public partial class MenuViewController : UIViewController, IUITableViewDelegate, IUITableViewDataSource { ... }

In the ViewDidLoad() I do:
TableView.Delegate = this; TableView.DataSource = this;

With this, RowsInSection() it called but always with section at 0 (don't know why it is called 4 times!).
GetCell() is also properly called, but still with section at 0.

All other methods are not called AFAICT:

  • NumberOfSections()
  • TitleForHeader()
  • GetHeightForHeader()

Tried with Weak version of these props but still doesn't work.

There is no problem if the controller inherits from UITableViewController.

Am I missing something?



  • rob8861rob8861 USMember ✭✭

    Don't use the Interfaces.
    What you want to do instead is create a new class and subclass UITableViewSource and override the required methods.
    then in your ViewController do the following:
    TableView.Source = new YourDataSourceClass();

    UITableViewSource merges both the source and delegate so you don't need to specify that. it is enough to just set the source property and both the source and delegate methods will be called.

    that should solve your issue.

  • ChrisApersChrisApers USMember

    Hi rob,

    Thank you for your answer. This indeed workarounds the issue.
    This forces me to move 99% of my controller code to this external class which doesn't really make sense as this Delegate/DataSource is the only purpose of it....

    It doesn't really fix it but I can now progress in my app.

    Is this a know long standing bug with Xamarin?


Sign In or Register to comment.