UITableViewCell中的WKWebView消失了内容

时间:2017-10-16 17:03:13

标签: swift uitableview webview wkwebview

我尝试设置TableView以显示我的内容,如下所示: 1 [HEADING PHOTO] 2.来自URL的内容 3. Json的相关项目 4. [FOOTER]

单元格1正常工作,对于单元格2,我有一个动态高度,一旦重新加载内容就会更新。但是,如果我的内容(以及单元格)大于我的屏幕,则不会显示。仅在缩放手势上才会显示文本。我可以向下滚动,因为Cell和WebView的大小合适。当我向后滚动到顶部时,我的屏幕外的文字再次被切断,并以缩放手势显示。

这是我的webview加载到我的单元格

class ArticleDetailPost: UITableViewCell, WKNavigationDelegate, UIScrollViewDelegate {

    var articleDetailController = ArticleDetailController(style: .grouped)
    var indexPath: IndexPath?

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: "cellId")
        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    var article: Article? {
        didSet {
            if let bind_id = article?.bind_id {
                webView.navigationDelegate = self
                webView.scrollView.delegate = self
                let base = URL(string: "http://dev.cubrass.nl/app.php?article_id="+bind_id)
                let url = URLRequest(url: base!)
                webView.load(url)
            }
        }
    }

    let webView: WKWebView = {
       let wv = WKWebView()
        wv.translatesAutoresizingMaskIntoConstraints = false
        wv.isOpaque = false
        wv.backgroundColor = .clear
        wv.scrollView.backgroundColor = .clear
        wv.scrollView.isScrollEnabled = false
        wv.scrollView.bounces = false
        wv.alpha = 0.0
        return wv
    }()

    func setupViews() {
        addSubview(webView)
        webView.leftAnchor.constraint(equalTo: self.leftAnchor).isActive = true
        webView.rightAnchor.constraint(equalTo: self.rightAnchor).isActive = true
        webView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
        webView.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
    }

    func viewForZooming(in: UIScrollView) -> UIView? {
        return nil;
    }

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {

        if webView.isLoading == false {

            webView.evaluateJavaScript("document.body.offsetHeight", completionHandler: { [weak self] (result, error) in
                if let height = result as? CGFloat {

                    let newHeight = height + 200
                    webView.frame.size.height += newHeight

                    print("scrollheight ",webView.scrollView.contentSize.height)
                    print(ArticleDetailController.contentHeights[(self?.indexPath?.row)!])

                    if (ArticleDetailController.contentHeights[(self?.indexPath?.row)!] != 0.0 &&
                            ArticleDetailController.contentHeights[(self?.indexPath?.row)!] == newHeight
                        )
                    {
                        webView.alpha = 1.0
                        return
                    }
                    let indexPath = self?.indexPath
                    ArticleDetailController.contentHeights[(self?.indexPath?.row)!] = newHeight

                    self?.articleDetailController.tableView.reloadRows(at: [indexPath!], with: .automatic)
                }
            })
        }
    }
}

这是我的cellForRowAt

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! ArticleDetailPost
    cell.article = article
    cell.articleDetailController = self
    cell.indexPath = indexPath
    cell.selectionStyle = UITableViewCellSelectionStyle.none;
    cell.backgroundColor = .clear
    return cell
}

寻找解决问题的解决方案,我只需滚动tableview并查看webview中的所有内容,而无需使用缩放手势进行渲染。

0 个答案:

没有答案