【问题标题】:MonoTouch.Dialog: Update of Text in an ElementMonoTouch.Dialog:更新元素中的文本
【发布时间】:2012-01-19 18:06:04
【问题描述】:

使用MonoTouch.Dialog 我添加StyledStringElement 元素。

有一个后台任务检索需要更新element.Value的细节

有没有办法强制元素在element.Value 更新后更新其文本?

伊恩

【问题讨论】:

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


    【解决方案1】:

    另一种更新标签的方法是从StyledStringElementStringElement 派生并直接刷新单元格内的DetailTextLabel:

    class UpdateableStringElement : StringElement
    {
        public UpdateableStringElement(string name): base (name)
        {
        }
    
        UILabel DetailText = null;
    
        public void UpdateValue(string text)
        {
            Value = text;
            if (DetailText != null)
                DetailText.Text = text;
        }
    
        public override UITableViewCell GetCell(UITableView tv)
        {
            var cell = base.GetCell(tv);
            DetailText = cell.DetailTextLabel;
            return cell;
        }
    }
    

    您可以使用UpdateValue 方法代替Value 属性:

    var element = new UpdateableStringElement("demo");
    
    SomeEventOfYours += delegate {
        element.UpdateValue(LocalizedValue);
    };
    

    【讨论】:

      【解决方案2】:

      我在 SetValueAndUpdate 方法中添加了“this.PrepareCell (cell); 并且可以正常工作。但我仍然认为还有另一个更好的选择来检测“caption”的变化并调用 this.PrepareCell (cell);。

      public void UpdateCaption(string caption) {
              this.Caption = caption;
              Console.WriteLine ("actualizando : " + Caption);
              UITableViewCell cell = this.GetActiveCell ();
              if (cell != null) {
                  this.PrepareCell (cell);
                  cell.SetNeedsLayout ();
              }
      }
      

      【讨论】:

        【解决方案3】:

        如果你想逐个元素更新这个元素,那么你可以使用类似的东西:

        public class MyStyledStringElement {
        
            public void SetValueAndUpdate (string value)
            {
                Value = value;
                if (GetContainerTableView () != null) {
                    var root = GetImmediateRootElement ();
                    root.Reload (this, UITableViewRowAnimation.Fade);
                }
            }
        }
        

        一种变体是加载所有内容并更新一次(即针对每个Element 迭代root.Reload)。

        【讨论】:

        • 您将如何以编程方式迭代元素?
        • 这对我不起作用,Caption 值未更新。我在一个非常特殊的情况下也有类似的问题。当我添加元素并更改标题属性后,不会更新“标题”标签。为什么?,因为元素只是在屏幕的底部,并且对控制器来说并不是完全不可见的。因此,如果我向上滚动 tableview 以使边框元素可见,则标题似乎未更新。我必须拉下 tableview 以使边框元素不完全可见以强制更新。
        • 很难说没有看到一些代码(你应该创建问题并提供更多数据)。屏幕外时 正确 的事实是单元格被缓存(由 iOS),因此在某些时候它们将被重用(用于其他目的),当你回到它们时,它不再是同一个单元格(它已使用最新值重新创建)。这是完全正常的。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 2014-04-17
        相关资源
        最近更新 更多