2013-10-26 2 views
1

나는 내 자신의 DialogViewController 클래스를 만들었습니다. 대화 상자에는 두 가지 레벨이 있습니다. 사용자가 편집 버튼을 클릭하여 두 번째 레벨의 요소를 제거 할 수있게하려고합니다. Monotouch.Dialog RootElement의 CommitEditingStyle

나를 일부 코드를 설명하려고하자

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-edit-mode-td4658436.html

를하지만, 같은에서 소스를 sublass하는 것도 가능합니다 두 번째 수준 (두 번째 수준 요소 1)의 방식?

+0

소스를 편집 할 수 있도록 코드를 표시 할 수 있습니까? – therealjohn

+0

소스 요소를 포함하도록 코드를 업데이트했습니다. – Nessinot

답변

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

당신은 당신이 요소를 제거하거나 안 경우 조작이 변수를 사용할 수 있습니다.

+0

시간 내 주셔서 감사합니다. 불행히도 귀하의 제안은 작동하지 않습니다. CommitEditingStyle 메서드는 두 번째 수준에있을 때 호출되지 않습니다. 원본은 TableView에 첫 번째 수준으로 설정되어 있기 때문에 용의자. 두 번째 수준에는 필자가 어떻게 든 소스에 연결해야하는 새로운 TableView가 있습니다. 나는 방법을 모른다. – Nessinot

+0

여러 개의 RootElements가 있어야합니까? 섹션을 사용해야하는 이유는 무엇입니까? – therealjohn

+0

휴가 때문에 나는 더 일찍 대응할 수 없었다. 계층 구조이기 때문에이 설치가 필요합니다. 첫 번째 레벨 1을 터치 한 후 새 DVD를 만들려고 할 수 있습니다. – Nessinot