c# - 为什么其中一些 Monotouch.Dialog 单元格显示为空?

标签 c# ios xamarin.ios monotouch.dialog

我有一个我认为非常简单的 Monotouch.Dialog 类。

public partial class EditAccountDialog : DialogViewController
{
    Section emailSection;
    Section passwordSection;
    Section profileSection;
    Section addressSection;

    EntryElement emailEntry;
    EntryElement passwordEntry;
    EntryElement password2Entry;
    EntryElement firstNameEntry;
    EntryElement lastNameEntry;
    EntryElement phoneNumberEntry;
    EntryElement streetEntry;
    EntryElement street2Entry;
    EntryElement cityEntry;
    EntryElement stateEntry;
    EntryElement zipEntry;

    public EditAccountDialog(bool pushing) : base (UITableViewStyle.Grouped, null, pushing)
    {
        emailEntry = new EntryElement(null, "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dcb9a4bdb1acb0b99cb8b3b1bdb5b2f2bfb3b1" rel="noreferrer noopener nofollow">[email protected]</a>", String.Empty);
        passwordEntry = new EntryElement (null, "Password", String.Empty, true);
        password2Entry = new EntryElement (null, "Re-enter password", String.Empty, true);
        firstNameEntry = new EntryElement ("First Name", "First Name", String.Empty);
        lastNameEntry = new EntryElement ("Last Name", "Last Name", String.Empty);
        phoneNumberEntry = new EntryElement ("Phone Number", "###-###-####", String.Empty);
        streetEntry = new EntryElement ("Street", "#### Any St.", String.Empty);
        street2Entry = new EntryElement ("Street 2", "Apt #", String.Empty);
        cityEntry = new EntryElement ("City", "City", String.Empty);
        stateEntry = new EntryElement ("State", "State", String.Empty);
        zipEntry = new EntryElement ("ZIP Code", "#####", String.Empty);

        emailSection = new Section ("Email"){
            emailEntry,
        };

        passwordSection = new Section ("Password"){
            passwordEntry,
            password2Entry,
        };

        profileSection = new Section("Profile") {
            firstNameEntry,
            lastNameEntry,
            phoneNumberEntry,
        };

        addressSection = new Section("Address") {
            streetEntry,
            street2Entry,
            cityEntry,
            stateEntry,
            zipEntry,
        };

        Root = new RootElement("Edit Account") {
            emailSection,
            passwordSection,
            profileSection,
            addressSection,
        };
    }

    public virtual void HandleGetAccountResponse(GetAccountResponse response)
    {
        emailEntry.Value = response.Email;
        firstNameEntry.Value = response.FirstName;
        lastNameEntry.Value = response.LastName;
        phoneNumberEntry.Value = response.Phone;
        streetEntry.Value = response.StreetAdd;
        street2Entry.Value = response.StreetAdd2;
        cityEntry.Value = response.City;
        stateEntry.Value = response.State;
        zipEntry.Value = response.Zip;
    }
}

页面加载后,我异步调用现有帐户信息的 REST API,然后调用上面的 HandleGetAccountResponse 来预先填充对话框中的每个 EntryElement

我检查了 REST 响应并知道我正在接收所有必要的数据。我遇到的问题是,此页面上的一两个随机单元格似乎是空白的,即使在设置了它们的值之后也是如此。例如,邮政编码和城市字段可能显示为空白。

更令人困惑的是,如果我滚动到底部并看到邮政编码和城市字段为空白,然后一直向上滚动,然后再次滚动到底部,就会出现不同 一组单元格可能为空,例如 Street2 和 State。

显然这不是 Monotouch.Dialog 的正常行为,否则没有人会使用它。我该如何解决这个问题?

最佳答案

我敢打赌,问题是由密码 EntryElements 使用与其他条目元素相同的“CellKey”(Objective-C 术语中的“reusableIdentifier”)引起的。

作为一种解决方法,我建议像这样子类化 EntryElement:

public class PasswordEntryElement : EntryElement
{
    static readonly NSString PasswordEntryElementCellKey = new NSString ("PasswordEntryElement");

    public PasswordEntryElement (string caption, string placeholder, string value) : base (caption, placeholder, value, true)
    {
    }

    protected override NSString CellKey {
        get { return PasswordEntryElementCellKey; }
    }
}

如果这有效,那么这是 MonoTouch.Dialog 中的一个错误(一旦您确认这对您有效,我将向官方 MonoTouch.Dialog github 项目提交拉取请求)。

问题是,如果配置不同,每种类型的单元确实需要有自己的“CellKey”。由于密码 EntryElements 使用不同配置的 UITextFields,这可能是导致您的问题的原因。

有用的提示:如果您遇到此类问题,其中某些单元格为空白,几乎总是由于多种类型的单元格重复使用相同的 CellKey。在您的例子中,您有 2 个单元格通常是不可见的 - 这强烈表明有 2 个元素与其他元素不同(这就是导致我选择密码元素的原因)。

关于c# - 为什么其中一些 Monotouch.Dialog 单元格显示为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13706729/

相关文章:

c# - 将窗口设置为在MVVM中隐藏后捕获屏幕

ios - 解析查询中一个键的两个约束

ios - 将数据追加到 UITableView 的正确方法,swift

javascript - 根据 react-native 侧边栏菜单中的选择在主视图中显示片段。

c# - 如何从 xamarin.forms 调用平台特定页面

c# - 如何使用 Monotouch for iPad 创建圆形按钮?

c# - 使用 "extra"变量是否错误,因为它更容易调试?

c# - 两个列表的区别 C#

c# - 在 WPF 中使用 ToolBarTray 控件进行数据绑定(bind)

c# - 处理全局异常机器人 | iOS