title: Display UIWebView in custom MonoTouch.Dialog Element
body: <p>Working on a way to display HTML and RTF content in a custom <code>Element</code> in <code>MonoTouch.Dialog</code>. To get started, I created a new class based on the <code>UIViewElement</code> implementation, with the intention of displaying a <code>UIWebView</code> in a cell (<a href="https://github.com/migueldeicaza/MonoTouch.Dialog/blob/master/MonoTouch.Dialog/Elements.cs" rel="nofollow">see UIViewElement code here</a>). </p>
  
  <p>The custom element works, but the contents of the <code>UIWebView</code> is never displayed. Once the text of the <code>UIWebView</code> is loaded, I try to resize it using <code>UIWebView.SizeThatFits</code>. This function always return 0s, even though the width given to it (from the actual <code>UITableViewCell</code> it is displayed in) is non-zero.</p>
  
  <p>Is it sufficient to add the <code>UIWebView</code> to the <code>UITableViewCell</code> using this code:</p>
  
  <pre><code>cell.ContentView.AddSubview (webView);
  </code></pre>
  
  <p>The <code>UIWebView</code> never returns a non-zero height, so the actual cell is never displayed. Even if I override the height calculation and return a static height (such as <code>100f</code>), the <code>UIWebView</code> is still not displayed.</p>
  
  <p>The element's complete code:</p>
  
  <pre><code>public class RBHTMLRow : Element, IElementSizing
  {
      public enum CellFlags {Transparent = 1,DisableSelection = 2}
      public CellFlags Flags;
      private UIWebView webView = null;
      private UITableViewCell current_cell;
      NSString key;
  
      private void ResizeWebView ()
      {
          if (current_cell != null)
          {
              RectangleF frame = webView.Frame;
              SizeF fittingSize = webView.SizeThatFits(new SizeF(current_cell.Frame.Width, 1f));
              frame.Size = fittingSize;
              webView.Frame = frame;
          }
      }
  
      private void InitWebView(bool isRTF, string content )
      {
          webView = new UIWebView();
          webView.LoadHtmlString ( content, new NSUrl(""));
          webView.LoadFinished += delegate {
              ResizeWebView();
          } ;
      }
  
      public RBHTMLRow (bool isRTF, String content, String caption) : base (caption)
      {
          key = new NSString("rbhtml_row");
          InitWebView(isRTF, content);
      }
  
      protected override NSString CellKey {
      get {
          return key;
      }
      }
  
      public override UITableViewCell GetCell (UITableView tv)
      {
          var cell = tv.DequeueReusableCell (CellKey);
          if (cell == null){
              cell = new UITableViewCell (UITableViewCellStyle.Default, CellKey);
              if ((Flags &amp; CellFlags.Transparent) != 0){
                  cell.BackgroundColor = UIColor.Clear;
                  cell.BackgroundView = new UIView (RectangleF.Empty) { 
                      BackgroundColor = UIColor.Clear 
                  } ;
              }
  
              cell.SelectionStyle = UITableViewCellSelectionStyle.None;
              cell.ContentView.AddSubview (webView);
          }  
          current_cell = cell;
          ResizeWebView();
          return cell;
      }
  
  public float GetHeight (UITableView tableView, NSIndexPath indexPath){
      return webView.Bounds.Height;
  }
  </code></pre>