Working on a way to display HTML and RTF content in a custom Element in MonoTouch.Dialog. To get started, I created a new class based on the UIViewElement implementation, with the intention of displaying a UIWebView in a cell (see UIViewElement code here).
The custom element works, but the contents of the UIWebView is never displayed. Once the text of the UIWebView is loaded, I try to resize it using UIWebView.SizeThatFits. This function always return 0s, even though the width given to it (from the actual UITableViewCell it is displayed in) is non-zero.
Is it sufficient to add the UIWebView to the UITableViewCell using this code:
cell.ContentView.AddSubview (webView);
The UIWebView 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 100f), the UIWebView is still not displayed.
The element's complete 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 & 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;
}
来源:https://stackoverflow.com/questions/12431208/display-uiwebview-in-custom-monotouch-dialog-element