1

我创建了自己的 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)中以相同的方式对源进行子分类?

4

1 回答 1

0

您可以自定义 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);
}

如果您应该删除一个元素,您可以使用这些变量来操作。

于 2013-10-29T15:47:55.267 回答