MonoTouch.Dialog:高级编辑

Bil*_*ver 3 xamarin.ios monotouch.dialog

我正在尝试整理一个可编辑的详细视图,类似于iPhone默认联系人应用程序.

我有一个联系人的TableView,当我选择一个单元格时,我会激活一个可编辑的详细信息视图:

public override void Selected (DialogViewController dvc, UITableView tableView, NSIndexPath path)
{       
    var editingSection = new Section ("Entity") {
        new StringElement ("First name", "enter", _entity.FirstName),
        new StringElement ("Last name", "enter", _entity.LastName)
    };

    var root = new RootElement("Entity Entry") {
        editingSection
    };

    var entityEdit = new EntityEdit (root, true);
    ConfigEdit (entityEdit);            
    dvc.ActivateController(entityEdit);

}

void ConfigEdit (DialogViewController dvc)
{
    dvc.NavigationItem.RightBarButtonItem = new UIBarButtonItem (UIBarButtonSystemItem.Edit, delegate {
        dvc.TableView.SetEditing (true, true);
        ConfigDone (dvc);
    });
}

void ConfigDone (DialogViewController dvc)
{
    dvc.NavigationItem.RightBarButtonItem = new UIBarButtonItem (UIBarButtonSystemItem.Done, delegate {
        dvc.TableView.SetEditing (false, true);
        ConfigEdit (dvc);
    });
}
Run Code Online (Sandbox Code Playgroud)

我想要更改的行为是在ConfigEdit方法中.当我第一次显示视图时,编辑部分中的元素应该是StringElements.当我切换到编辑模式时,元素应该更改为条目元素,因为我希望能够删除行,或编辑元素内的文本.

这可能吗?在设置编辑模式之前,是否有更好的方法来显示只读元素?

mig*_*aza 6

你有几个选择:

一个.您只需使用更新的RootElement更改RootElement,或使用适当类型的元素的新单元格更新单个单元格以获得所需的效果.你可以通过参数化你的创作来做到这一点:

RootElement CreateRoot (bool editable)
{
    return new RootElement (...) {
         CreateEditableElement ("foo", bar, editable)
    }
}

Element CreateEditableElement (string caption, string content, bool editable)
{
     return editable ? EntryElement (caption, content) : StringELement (caption, content)
}
Run Code Online (Sandbox Code Playgroud)

然后,您需要在用户点击"编辑"按钮后调用ReloadData.

您可以做的另一件事是创建一个可以根据此信息切换状态的新元素.我的博客有一个关于如何创建新元素的教程:

http://tirania.org/monomac/archive/2011/Jan-18.html