Element
カスタムでHTML および RTF コンテンツを表示する方法に取り組んでいますMonoTouch.Dialog
。まず、セルUIViewElement
に を表示する目的で、実装に基づいて新しいクラスを作成しました( UIViewElement コードはこちらを参照してください)。UIWebView
カスタム要素は機能しますが、の内容はUIWebView
表示されません。のテキストUIWebView
がロードされたら、 を使用してサイズ変更を試みUIWebView.SizeThatFits
ます。この関数は、指定された幅 (実際UITableViewCell
に表示されているものから) がゼロでない場合でも、常に 0 を返します。
このコードを使用してUIWebView
にを追加するだけで十分ですか:UITableViewCell
cell.ContentView.AddSubview (webView);
はUIWebView
ゼロ以外の高さを決して返さないため、実際のセルは表示されません。高さの計算をオーバーライドして静的な高さ ( など100f
) を返しても、UIWebView
は表示されません。
要素の完全なコード:
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;
}