C# Monotouch.Dialog根元素的CommittedItingStyle
我已经创建了自己的DialogViewController类。该对话框有两个级别。我希望用户能够单击一个编辑按钮,允许他删除第二级的元素 让我试着用一些代码来解释:C# Monotouch.Dialog根元素的CommittedItingStyle,c#,iphone,monotouch.dialog,C#,Iphone,Monotouch.dialog,我已经创建了自己的DialogViewController类。该对话框有两个级别。我希望用户能够单击一个编辑按钮,允许他删除第二级的元素 让我试着用一些代码来解释: public class TestMenu : DialogViewController { public TestMenu() : base (new RootElement("Menu"), true) { Section section = new Section (); thi
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);
}
}
}
当用户单击编辑单元格时,表格设置为编辑模式
点击删除图标当然没有任何作用。如何启用编辑模式或滑动以在第二级及以后的根元素上显示删除按钮
我已经阅读了以下文章,其中解释了如何在dialog view controller第一个屏幕中启用编辑模式:
这适用于第一级,但也可以在第二级第二级元素1中以相同的方式对源进行子类化?您可以自定义CommittedItingStyle方法来执行任何您想要的操作,而不是调用代码所显示的基本实现 例如:
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);
}
您可以使用这些变量来操作是否应该删除元素 您能在启用编辑的地方显示源代码吗?我已经更新了代码以包含源元素。感谢您抽出时间回答我的问题。不幸的是,你的建议不起作用。当我处于第二级时,不会调用CommittedItingStyle方法。我怀疑是因为源设置为第一级的TableView。第二层有一个新的TableView,我必须以某种方式连接到源代码。我只是不知道怎么做。你需要有多个根元素吗?为什么不直接使用分区呢?因为有个假期,我没法尽快回复。我需要这个设置,因为它是一个层次结构。我可以尝试在触摸第一级1后创建一个新的dvc。