【发布时间】:2012-01-19 18:06:04
【问题描述】:
使用MonoTouch.Dialog 我添加StyledStringElement 元素。
有一个后台任务检索需要更新element.Value的细节
有没有办法强制元素在element.Value 更新后更新其文本?
伊恩
【问题讨论】:
标签: c# ios xamarin.ios monotouch.dialog
使用MonoTouch.Dialog 我添加StyledStringElement 元素。
有一个后台任务检索需要更新element.Value的细节
有没有办法强制元素在element.Value 更新后更新其文本?
伊恩
【问题讨论】:
标签: c# ios xamarin.ios monotouch.dialog
另一种更新标签的方法是从StyledStringElement 或StringElement 派生并直接刷新单元格内的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);
};
【讨论】:
我在 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 ();
}
}
【讨论】:
如果你想逐个元素更新这个元素,那么你可以使用类似的东西:
public class MyStyledStringElement {
public void SetValueAndUpdate (string value)
{
Value = value;
if (GetContainerTableView () != null) {
var root = GetImmediateRootElement ();
root.Reload (this, UITableViewRowAnimation.Fade);
}
}
}
一种变体是加载所有内容并更新一次(即针对每个Element 迭代root.Reload)。
【讨论】: