【问题标题】:Why are some of these Monotouch.Dialog cells appearing empty?为什么其中一些 Monotouch.Dialog 单元格显示为空?
【发布时间】:2012-12-04 15:53:10
【问题描述】:

我有一个我认为非常简单的 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, "example@domain.com", 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 响应并知道我正在接收所有必要的数据。我遇到的问题是此页面上的一两个随机单元格似乎是空白的,即使在设置了它们的值之后也是如此。例如,邮编和城市字段可能显示为空白。

更令人困惑的是,如果我滚动到底部看到 Zip 和 City 字段为空白,然后一直向上滚动,然后再次滚动到底部,不同 strong> 一组单元格可能是空白的,例如 Street2 和 State。

显然这对于​​ Monotouch.Dialog 来说是不正常的行为,否则没人会使用它。我可以做些什么来解决这个问题?

【问题讨论】:

  • 只有单元格值是空的,还是标签也是空的?您最好的选择可能是使用 MT.D 源代码(来自 github)并对其进行调试。或者,如果您可以提出一个孤立的测试用例,请向 Xamarin 提交错误。
  • 这些值不是空的,它们只是在屏幕上不可见。我应该在问题标题中澄清一下。
  • 这听起来像是 MT.D 中可能存在的错误 - 如果您可以创建一个独立的测试用例,我会将其提交给 Xamarin
  • 我想出了一个并昨天提交给他们,但还没有收到回复。
  • 这通常发生在您有不同类型的单元格重用相同的 reusableIdentifierKey 时,但似乎您的所有元素都是 EntryElements,所以这应该不是问题。

标签: c# ios xamarin.ios monotouch.dialog


【解决方案1】:

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

作为一种解决方法,我建议像这样子类化 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 个元素与其他元素不同(这就是导致我使用密码元素的原因)。

【讨论】:

  • 我会试一试,但密码元素从来都不是空白的(至少据我所知)。它始终是最底部的单元格之一。
  • 这很有趣...将密码元素切换到您建议的子类消除了一个空白元素,但仍然总是出现一个。您还有其他建议吗?
  • 我不知道,也许是 emailEntry,因为您将标题设置为空?顺便说一句,空白(或非空白)的元素不一定与导致问题的单元格相关,您可以依赖的只是空白单元格的数量。
【解决方案2】:

根据 Xamarin 工程师发回给我的测试样本,我最终能够通过更改以下几行来解决此问题:

    emailEntry = new EntryElement(null, "example@domain.com", String.Empty);
    passwordEntry = new EntryElement (null, "Password", String.Empty, true);
    password2Entry = new EntryElement (null, "Re-enter password", String.Empty, true);

到这里:

    emailEntry = new EntryElement(" ", "example@domain.com", String.Empty);
    passwordEntry = new EntryElement (" ", "Password", String.Empty, true);
    password2Entry = new EntryElement (" ", "Re-enter password", String.Empty, true);

请注意只有一个空格的字符串。我尝试使用String.Empty 或像"" 这样的空字符串,这是更直观的想法,但它们表现出同样的问题。

【讨论】:

    【解决方案3】:

    pdusen,在模型更新后尝试将InvokeOnMainThread(ReloadData) 放置在HandleGetAccountResponse 中。希望对您有所帮助!

    【讨论】:

    • 另一个人发布了同样的答案,但似乎已将其删除。正如我告诉他们的,这并不能解决问题。另外,请注意,上面的大多数字段确实显示了收到的配置文件数据;只有少数随机出现空白。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    • 2020-02-05
    • 2021-07-10
    • 1970-01-01
    相关资源
    最近更新 更多