0

简单来说,我想使用 UIView.AddSubview 将 Monotouch.Dialog.Element 添加到 UIView。我需要更改/创建什么才能使这成为可能?

4

1 回答 1

1

AMonoTouch.Dialog.Element基于 a UITableViewCell,而不是 a UIView

因此,Element应该是 a 的一部分,UITableView不能简单地添加到 aUIView作为 a SubView

如果你想要一个UIView类似于 的Element,你必须创建一个自定义视图,继承自UIView。在此视图中,您可以从您选择的内部创建您喜欢的View行为。UITableViewCellElement

编辑:基于 MultiLineElement 的基本示例

public class MyView : UIView
{
  private string Caption { get; set; }          
  private string Text { get; set; }

  public View(string caption, string text) : base()
  {
    Opaque = true;
    BackgroundColor = UIColor.Clear;
    Update(caption, text);
  }

  public void Update(string caption, string text)
  {
    Caption = caption;
    Text = text;

    SetNeedsDisplay();
  }

  public override void Draw(RectangleF frame)
  {
    var bounds = Bounds;
    var captionFont = UIFont.BoldSystemFontOfSize(12f);
    var textFont = UIFont.SystemFontOfSize(10f);
    var width = Bounds.Width;

    if (string.IsNullOrWhiteSpace(Caption) == false)
    {
      // Caption, black
      UIColor.Black.SetColor();
      width = Bounds.Width / 2;
      var captionHeight = 
        StringSize(Caption, captionFont, width, UILineBreakMode.TailTruncation).Height;
      var captionOffset = textFont.PointSize - captionFont.PointSize;
      DrawString(Caption, new RectangleF(0, captionOffset, width, captionHeight),
        captionFont, UILineBreakMode.TailTruncation, UITextAlignment.Right);
    }

    // Text, dark gray
    UIColor.DarkGray.SetColor();
    var textHeight = 
      StringSize(Text, textFont, width, UILineBreakMode.WordWrap).Height;
    DrawString(Text, new RectangleF(Bounds.Width - width, 0, width, textHeight), 
      textFont, UILineBreakMode.WordWrap);
  }
}
于 2013-02-18T20:49:01.657 回答