monotouch UITableViewDelegate RowSelected event ca

2019-06-14 02:38发布

问题:

I am using a custom UITableViewDelegate and in my controller I want to run some code when the tableview has a rowselected. I noticed the UITableViewDelegate already has an event called RowSelected but you cannot use it I'm guessing because there is a method in UITableViewDelegate with the exact same name.

If I write:

mytableviewdelegate.RowSelected += myeventhandler;

This will not compile and gives the error:

"Cannot assign to 'RowSelected' because it is a 'method group'"

Any ideas, I have a work around which is fine so Im really looking at working out if this is a bug in MonoTouch?

回答1:

How are you implementing the custom UITableViewDelegate? I would suggest using Monotouch's UITableViewSource as it combines both the UITableViewDataSource and the UITableViewDelegate into one file which makes things so much easier.

Some example code:

(in your UIViewController that contains the UITableView)

tableView.Source = new CustomTableSource();

Then you'll want to create a new class for this:

public class CustomTableSource : UITableViewSource
{
    public CustomTableSource()
    {
        // constructor
    }
    // Before you were assigning methods to the delegate/datasource using += but
    // in here you'll want to do the following:
    public override int RowsInSection (UITableView tableView, int section)
    {
        // you'll want to return the amount of rows you're expecting
        return rowsInt;
    }

    // you will also need to override the GetCells method as a minimum. 
    // override any other methods you've used in the Delegate/Datasource 
    // the one you're looking for in particular is as follows:

    public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
    {
        // do what you need to here when a row is selected!
    }
}

That should help you get started. In the UITableViewSource class you can always type public override and MonoDevelop will show you what methods are available to be overriden.



标签: xamarin.ios