【问题标题】:MonoTouch.Dialog : Element Delete EventMonoTouch.Dialog : 元素删除事件
【发布时间】:2011-03-20 05:26:56
【问题描述】:
使用 Miguel de Icaza 的 Patterns for Creating UITableViewCells,我创建了一个自定义 UITableViewCell 并将其转换为 MonoTouch.Dialog 元素。我正在使用元素 API 创建一个编辑表单,其中使用了一些我的自定义元素。
我试图弄清楚如何响应元素的删除。我的自定义元素引用了它在数据库中表示的记录。我想以与响应 Selected 事件相同的方式响应已删除事件,在该事件中我得到 DialogViewController、UITableView 和 NSIndexPath。假设我可以响应的元素存在这样的事件,我会使用给定的记录 ID 向数据库触发删除语句。
【问题讨论】:
标签:
xamarin.ios
monotouch.dialog
【解决方案1】:
根据 Miguel 的回答,我向名为 MyDataElement 的子类 Element 添加了一个公共 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));
}
}
然后在我的子类 DialogViewController 上,我处理 CommitEditingStyle 方法,将元素转换为 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);
}
}
【解决方案2】:
您必须修改源以处理 Source 类中的删除事件并将该消息分派给元素,其方式与处理其他事件的方式相同。