繁体   English   中英

Monotouch.Dialog RootElement的CommitEditingStyle

[英]CommitEditingStyle of Monotouch.Dialog RootElement

我创建了自己的DialogViewController类。 该对话框有两个级别。 我希望用户能够单击一个编辑按钮,使他可以删除第二级上的元素。

让我尝试用一​​些代码来解释:

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);
        }
    }
}

当用户单击“编辑”单元格时,表将设置为编辑模式。

当然,单击删除图标不会执行任何操作。 如何启用编辑模式或滑动以在第二级及以后的根元素上显示删除按钮?

我已阅读以下文章,该文章解释了如何在对话框视图控制器的第一个屏幕中启用编辑模式: http : //monotouch.2284126.n4.nabble.com/Monotouch-Dialog-table-rows-not-selectable-in-编辑模式td4658436.html

这适用于第一层,但是也可以在第二层(第二层元素1)中以相同的方式对Source进行细分。

您可以自定义CommitEditingStyle方法以执行所需的任何操作,而不是调用代码所显示的基本实现。

例如:

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);
}

如果应删除元素,则可以使用这些变量进行操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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