0

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;
}
4

0 に答える 0