开发者

MonoTouch.Dialog : Element Delete Event

Using Miguel de Icaza's Patterns for Creating UITableViewCells, I have created a custom UITableViewCell and turn开发者_StackOverflow中文版ed that into a MonoTouch.Dialog Element. I'm using the elements API to create an edit form, using a few of my custom elements.

I'm trying to figure out how to respond to the deletion of the element. My custom element has a reference to the record it represents in the database. I want to respond to a deleted event in the same way I would respond to a Selected event, where I get the DialogViewController, UITableView, and NSIndexPath. Assuming such an event existed for an Element that I can respond to, I would fire a delete statement to the database with the given record id.


Based on Miguel's answer, I added a public Delete method to the sub classed Element called MyDataElement.

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

Then over on my sub classed DialogViewController, I handle the CommitEditingStyle method, cast the element as MyDataElement, then call the Delete method:

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

        }


You would have to modify the source to handle the delete event in the Source class and dispatch that message to the Element, in the same way that it is done for the other events.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜