简体   繁体   中英

CommitEditingStyle of Monotouch.Dialog RootElement

I have created my own DialogViewController class. The dialog has two levels. I want the user to be able to click an edit button that allows him to remove the elements on the second level.

Let me try to explain with some code:

public class TestMenu : DialogViewController
{
    public TestMenu() : base (new RootElement("Menu"), true)
    {
        Section section = new Section ();
        this.Root.Add (section);
        RootElement firstRoot = new RootElement ("First level 1");
        section.Add (firstRoot);
        RootElement secondRoot = new RootElement ("First level 2");
        section.Add (secondRoot);

        // first rootelement
        Section firstSection = new Section ();
        firstRoot.Add (firstSection);
        StringElement firstElement = new StringElement ("Second level element 1");
        firstSection.Add (firstElement);

        // Button to set edit mode
        Section buttonSection = new Section ();
        firstRoot.Add (buttonSection);
        StringElement buttonElement = new StringElement ("Edit");
        buttonElement.Tapped += delegate
        {
            // This works to get it in editing mode
            firstRoot.TableView.SetEditing(true, true);

            // This statement will not set it to editing mode
            //this.SetEditing(true, true);
        };
        buttonSection.Add (buttonElement);

        // second rootelement
        Section secondSection = new Section ();
        secondRoot.Add (secondSection);
        StringElement secondElement = new StringElement ("Second level element 2");
        secondSection.Add (secondElement);
    }

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

    class TestSource : DialogViewController.SizingSource 
    {

        public TestSource(DialogViewController container)
            : base (container)
        {}

        public override bool CanEditRow (UITableView tableView, NSIndexPath indexPath)
        {
            return true;
        }

        public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
        {
            // This is only fired when something is deleted in the first level
            base.CommitEditingStyle (tableView, editingStyle, indexPath);
        }
    }
}

When the user clicks on the Edit cell the table is set in edit mode.

Clicking on the delete icons of course does nothing. How can I enable edit mode or swipe to show the delete button on the root elements in the second level and onwards?

I have read the following post that explains how to enable the edit mode in the dialog view controller first screen: http://monotouch.2284126.n4.nabble.com/Monotouch-Dialog-table-rows-not-selectable-in-edit-mode-td4658436.html

This works for the first level, but it is also possible to sublass the Source in the same way in the second level (Second level element 1)?

You can customize the CommitEditingStyle method to do whatever you want, rather then calling the base implementation which is what your code shows.

For example:

public override void CommitEditingStyle(UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath)
{
    var section = Container.Root[indexPath.Section];
    var element = section[indexPath.Row];
    section.Remove(element);
    Container.Root.Reload(section, UITableViewRowAnimation.None);
}

You can use those variables to manipulate if you should remove an element or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM