UIWebView的全部内容嵌入在UITableView中

Osc*_*and 11 uiwebview uitableview ios autolayout swift

我已经为这两天苦苦挣扎了,所以我和互联网的聪明人一起参与其中.

我正在展示一篇文章作为我的一部分UITableView.为了正确显示,我需要给代表一个单元格的高度,我希望它与UIWebView高度相同,所以我可以在WebView上禁用滚动并将整个Web内容显示为静态细胞.

我的第一种方法是在heightForRowAtIndexpath方法中渲染它,但这显然不起作用,因为我需要等待UIWebViewDelegate告诉我何时Web视图完全加载并具有高度.过了一会儿,我发现了一个工作解决方案,它使用Web视图委托在加载Web视图时刷新单元格高度.

工作正常,直到屏幕大小改变.无论是旋转还是全屏我的UISplitView.我强制对它进行更新didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation),但是这会使它闪烁大约10次然后才能进入正确的高度.我记录了这个更改,似乎WebView多次调用自身,导致循环.

曾经出现在这个日志,从我旋转屏幕开始.

每次重新加载时它会闪烁一次,正如您所看到的,它会自动重新加载一次.

所以.我需要一种在uitableview中显示整个Web视图内容的方法,并在屏幕大小更改时可靠地获取高度.如果有人以前以任何方式管理过这个,请告诉我.我会给任何可以解决这个问题的人一个赏金和我的长子,因为它让我疯了.

这是我的相关代码.

func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    switch indexPath.row {
case 4:
        //Content
        print("Height for row called")
        return CGFloat(webViewHeight)
}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    switch (indexPath.row){
//HTML Content View
    case 4:
        let cell = tableView.dequeueReusableCellWithIdentifier("ContentCell", forIndexPath: indexPath)
        var contentCell = cell as? ContentCell
        if contentCell == nil {
            contentCell = ContentCell()
        }
        contentCell?.contentWebView.delegate = self
        contentCell?.contentWebView.scrollView.userInteractionEnabled = false
        contentCell?.contentWebView.loadHTMLString((post?.contentHTML)!, baseURL: nil)
        print("Cell For row at indexpath called")
        return contentCell!
}
func webViewDidFinishLoad(webView: UIWebView) {
    updateHeight()
}

func updateHeight(){
    let webView = (self.tableView.cellForRowAtIndexPath(NSIndexPath(forRow: 4, inSection: 0)) as! ContentCell).contentWebView
    if self.webViewHeight != Double(webView.scrollView.contentSize.height) {
        print("Previous WV Height = \(self.webViewHeight), New WV Height = \(webView.scrollView.contentSize.height)")
        self.webViewHeight = Double(webView.scrollView.contentSize.height)
        tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 4, inSection: 0)], withRowAnimation: .Automatic)
    } else {
        return
    }
}

override func didRotateFromInterfaceOrientation(fromInterfaceOrientation: UIInterfaceOrientation) {
    print("rotated")
        self.updateHeight()
        //tableView.reloadRowsAtIndexPaths([NSIndexPath(forRow: 4, inSection: 0)], withRowAnimation: .Automatic)
}
Run Code Online (Sandbox Code Playgroud)

Osc*_*and 1

我通过将行更改动画中的 .Automatic 更改为 .None 解决了这个问题。它仍然是一个糟糕的解决方案,但至少它不再闪烁。