2011-03-20 7 views
2

Miguel de Icaza의 Patterns for Creating UITableViewCells을 사용하여 사용자 지정 UITableViewCell을 만들고 MonoTouch.Dialog 요소로 변환했습니다. 내 사용자 정의 요소 중 몇 가지를 사용하여 요소 API를 사용하여 편집 양식을 만듭니다.MonoTouch.Dialog : Element Delete Event

나는 요소의 삭제에 어떻게 반응하는지 알아 내려고하고있다. 내 사용자 지정 요소는 데이터베이스에서 나타내는 레코드에 대한 참조를가집니다. DialogViewController, UITableView 및 NSIndexPath를 얻은 Selected 이벤트에 응답하는 것과 같은 방식으로 삭제 된 이벤트에 응답하고 싶습니다. 내가 응답 할 수있는 요소에 대해 이러한 이벤트가 있다고 가정하면 지정된 레코드 ID를 사용하여 데이터베이스에 delete 문을 실행합니다.

답변

3

소스를 수정하여 소스 클래스의 삭제 이벤트를 처리하고 해당 이벤트를 다른 이벤트와 동일한 방식으로 요소에 전달해야합니다.

4

Miguel의 답을 바탕으로 MyDataElement라는 하위 클래스 요소에 공용 Delete 메서드를 추가했습니다.

public class MyDataElement : Element { 
    static NSString key = new NSString ("myDataElement"); 
    public MyData MyData; 

    public MyDataElement (MyData myData) : base (null) 
    { 
      MyData = myData; 
    } 

    public override UITableViewCell GetCell (UITableView tv) 
    { 
      var cell = tv.DequeueReusableCell (key) as MyDataCell; 
      if (cell == null) 
       cell = new MyDataCell (MyData, key); 
      else 
       cell.UpdateCell (MyData); 
      return cell; 
    } 

    public void Delete() { 
     Console.WriteLine(String.Format("Deleting record {0}", MyData.Id)); 
    } 
} 

는 다음을 통해 내 하위에, 나는 CommitEditingStyle 방법을 처리, DialogViewController을 분류 MyDataElement은 다음 Delete 메서드 호출로 요소를 캐스팅 :

public class EntityEditingSource : DialogViewController.Source { 

      public EntityEditingSource(DialogViewController dvc) : base (dvc) {} 

      public override bool CanEditRow (UITableView tableView, NSIndexPath indexPath) 
      { 
       // Trivial implementation: we let all rows be editable, regardless of section or row 
       return true; 
      } 

      public override UITableViewCellEditingStyle EditingStyleForRow (UITableView tableView, NSIndexPath indexPath) 
      { 
       // trivial implementation: show a delete button always 
       return UITableViewCellEditingStyle.Delete; 
      } 

      public override void CommitEditingStyle (UITableView tableView, UITableViewCellEditingStyle editingStyle, NSIndexPath indexPath) 
      { 
       // In this method, we need to actually carry out the request 
       var section = Container.Root [indexPath.Section]; 
       var element = section [indexPath.Row]; 

       //Call the delete method on MyDataElement 
       (element as MyDataElement).Delete(); 
       section.Remove (element); 
      } 

     }