2

如何创建自定义 UIWebView 元素以根据 Html 内容自动调整其高度?

这是我的自定义 TableViewCell 类和自定义 MonoTouch 对话框元素代码。我试图从 TableViewCell 类中获取高度并将其发送回以设置 Element 类的高度。但是,我不知道该怎么做。

public class WebContentViewCell : UITableViewCell
{
    public static NSString Key = new NSString("WebContentViewCell");

    public WebContentViewCell(string htmlContent, UITableView tv)
        : base(UITableViewCellStyle.Default, Key)
    {
        View = new UIWebView();
        View.LoadHtmlString(htmlContent, null);
        ContentView.Add(View);
    }

    public void UpdateCell(string htmlContent)
    {
        View.LoadHtmlString(htmlContent, null);
    }

    public UIWebView View { get; set; }
    public float Height { get; set; }

    public override void LayoutSubviews()
    {
        base.LayoutSubviews();

        var contentFrame = ContentView.Bounds;
        View.Frame = contentFrame;
    }
}
public class WebContentElement : Element, IElementSizing
{
    private readonly string _htmlContent;
    private float _height = 200;
    private UIWebView _webView;

    public WebContentElement(string htmlContent)
        : base("")
    {
        _htmlContent = htmlContent;
        _webView = new UIWebView();
        _webView.LoadHtmlString(_htmlContent, null);
    }

    protected override UITableViewCell GetCellImpl(UITableView tv)
    {
        var cell = tv.DequeueReusableCell(WebContentViewCell.Key) ?? new WebContentViewCell(_htmlContent, tv);
        return cell;
    }

    public float GetHeight(UITableView tableView, NSIndexPath indexPath)
    {
        return _height; // how to get the height based on the WebContentViewCell's content?
    }
}
4

0 回答 0