Question

This is my first question, I apologize if it is not the best.

I am brand new to using Xamarin and iOS development. I have a custom implementation of DialogViewController so that I can override certain methods on UITableViewController. For the sake of the question, it is a very basic implementation.

public class CustomDialogViewController : DialogViewController
    {
        public CustomDialogViewController(RootElement root)
            : base (root)
        { }

        public override NSIndexPath WillSelectRow(UITableView tableView, NSIndexPath indexPath)
        {
            return base.WillSelectRow(tableView, indexPath);
        }

        public override NSIndexPath WillDeselectRow(UITableView tableView, NSIndexPath indexPath)
        {
            return base.WillDeselectRow(tableView, indexPath);
        }

        public override void RowSelected(UITableView tableView, NSIndexPath indexPath)
        {
            base.RowSelected(tableView, indexPath);
        }
    }

I create my instance like this:

var rootElement = new RootElement("Main");
_mainSection = new Section();
rootElement.Add(_mainSection);
dvcProductGroupList = new CustomDialogViewController(rootElement);
dvcProductGroupList.Style = UITableViewStyle.Plain;
dvcProductGroupList.TableView.Frame = new RectangleF(...);
dvcProductGroupList.View.AutoresizingMask = UIViewAutoresizing.FlexibleWidth;
dvcProductGroupList.OnSelection += dvcProductGroupList_OnSelection;

The DialogViewController is then added to the view. I put breakpoints at the start of each of those overrides but they are never hit. At the point of creation there are no elements but when the view loads it populates the dialog view controller with some data, 4 string elements. I would really appreciate any assistance in trying to get more control over row selection using MonoTouch Dialog. I am trying to get the effect of canceling a row change so that it doesn't select another row when clicked and I think those are the correct methods, I just don't understand why they are never getting hit and I feel like it's a really simple problem.

Thanks.

Was it helpful?

Solution

Whether or not I am approaching this in the correct way I am not sure but I did get the answer from someone regarding the exact issue I was having with the overridden methods not getting called, so I will post their answer in case anyone has the same issue. Credit to JonathanBird from the Xamarin Forums.

You are attempting to override these methods in the wrong class. DialogViewController uses a UITableViewSource object to build out the UITableView. My understanding is that when a UiTableViewSource is used to configure a UITableViewController then methods in the UITableViewController are not called. Since DialogViewController is a subclass of UITableViewController this applies to it as well.

DialogViewController uses a UITableViewSource object of subtype DialogViewController.Source. If you want to override the methods in question then you need to subclass DialogViewController.Source and override the methods there.

Finally, you will need to substitute the the DialogViewController.Source that the DialogViewController creates with the one you have created. You have an opportunity to do this by overriding CreateSizingSource () in your CustomDialogViewController.

DialogViewController uses the following implementation of CreateSizingSouce().

public virtual DialogViewController.Source CreateSizingSource (bool unevenRows)
{
    return (!unevenRows) ? new DialogViewController.Source (this) : new DialogViewController.SizingSource (this);
}

If your implementation does not make use of unevenRows then you only need to worry about returning one type of Source.

For example the override in your CustomDialogViewController might be:

public override Source CreateSizingSource (bool unevenRows)
{
    return new CustomSource(this);
}

Where CustomSource is your subclass of DialogViewController.Source which overrides the methods you desire.

Below is one of many possible forms your implementation might take which shows an override of the RowSelected method.

public class CustomDialogViewController : DialogViewController
{
    public CustomDialogViewController (RootElement root) : base (root) {}

    public override Source CreateSizingSource (bool unevenRows)
    {
        return new CustomSource(this);
    }

    private class CustomSource : DialogViewController.Source
    {
        public CustomSource (CustomDialogViewController controller) : base (controller)
        {}

        public override void RowSelected (UITableView tableView, NSIndexPath indexPath)
        {
            // You handle row selected here
            base.RowSelected (tableView, indexPath);
        }
    }
}

OTHER TIPS

These methods are meant to make the managed UITableViewController conform to the ObjC UITableViewDataSource protocol. You can then use:

this.TableView.WeakDataSource = this;

in your controller.

Using MT.Dialog you only deal with elements and only in rare cases with the underlying UITableView. Usually your elements have tap/click handlers. In those, perform your actions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top