2

Using MonoTouch.Dialog I add StyledStringElement elements.

There is a background task that retrieves details that need to update the element.Value

Is there a way to force the element to have it's text updated after the element.Value is updated?

Ian

poupou
  • 43,413
  • 6
  • 77
  • 174
Ian Vink
  • 66,960
  • 104
  • 341
  • 555

3 Answers3

12

If you want to update this element-by-element then you can use something like:

public class MyStyledStringElement {

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

A variation would be to load everything and update once (i.e. iterating on the root.Reload for every Element).

poupou
  • 43,413
  • 6
  • 77
  • 174
  • How would you programmatically iterate through elements? – BRogers Feb 25 '13 at 22:08
  • This do not works for me, the Caption value is not updated. I have a similar problem with a very particular case. When I've added the elements and after I'd changed the caption property is not updating the "caption" label. Why ?, because the element is just on the bottom of the screen and is not completly invisible to the controller. So, if I scroll up tableview to make visible the border element the caption appears not updated. I have to pull down the tableview in order to make the border element not visible completly to force the update. – rolivares May 27 '13 at 19:58
  • Hard to say without seeing some code (you should create question and provide more data). The fact that it's *correct* when offscreen is that cells are cached (by iOS) so at some point they will be reused (for something else) and when you get back to them it's not the same cell anymore (it's been recreated with the latest values). That's perfectly normal. – poupou May 27 '13 at 20:41
1

I've added "this.PrepareCell (cell); to the SetValueAndUpdate method and works. But I still thinking in that there is another better option detecting the change of "caption" and calling 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 ();
        }
}
rolivares
  • 137
  • 1
  • 3
  • 12
1

Another approach to update the label would be to derive from StyledStringElement or StringElement and directly refresh the DetailTextLabel within the cell:

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;
    }
}

Instead of the Value property you can then use the UpdateValue method:

var element = new UpdateableStringElement("demo");

SomeEventOfYours += delegate {
    element.UpdateValue(LocalizedValue);
};
Rodja
  • 7,998
  • 8
  • 48
  • 55