缩放时 UITableViewCell 中的更新约束无法与 UIWebView scrollView 一起正常工作

Updating constraint in UITableViewCell not working properly with UIWebView scrollView when zooming

我正在尝试更新 table 视图单元格的高度以匹配放大后 UIWebView 滚动视图的高度。UIWebView 是 table查看单元格。

func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
    DispatchQueue.main.async {
        print("\nscrollViewDidEndZooming")
        print("webView.scrollView.contentSize.height: \(self.webView.scrollView.contentSize.height)")
        print("webViewHeightConstraint: \(self.webViewHeightConstraint.constant)")

        let height = self.webView.scrollView.contentSize.height
        self.webViewHeightConstraint.constant = height
        myDelegate.reloadTableView()
    }
}

在父控制器中:

func reloadTableView() {
    tableView.beginUpdates()
    tableView.endUpdates() 
}

网络视图上加载了 html 内容。问题是,在调用 didEndZooming 并更新高度约束后,它没有正确设置。高度限制将增大直到。在我看来,在 tableView 单元格中设置高度限制会以某种方式创建反馈循环。

我怎样才能使它按我期望的方式工作? (它应该调整到缩放后的值,而滚动视图的大小不会无限制地增长。)

更新: 在

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: WebViewCell.description(), for: indexPath) as! WebViewCell

    //The cell delegate implements the didFinishLoad() function
    cell.delegate = self

    //TODO: I know this is not efficient, will update later.
    if let html = viewModel.emailInfo?.htmlContent {
        cell.loadHTMLString(html)
    }

    return cell
}

单元格本身在 xib 文件中。我暂时将视图放在虚拟视图中(试图查看这是否解决了我的问题(但没有解决)。网络视图在顶部、底部、左侧和右侧边缘有约束。网络视图也有高度约束。正是这个高度约束 webViewHeightConstraint,我试图用它来确定单元格的高度。

这是放大和缩小几个循环后的日志输出。这是在设置新高度约束之前打印的:

scrollViewDidEndZooming webView.scrollView.contentSize.height: 962.0 webViewHeightConstraint: 509.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 962.0 webViewHeightConstraint: 962.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 1531.0 webViewHeightConstraint: 962.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 1549.0 webViewHeightConstraint: 1531.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 2566.0 webViewHeightConstraint: 1549.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 2566.0 webViewHeightConstraint: 2566.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 4779.0 webViewHeightConstraint: 2566.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 4779.0 webViewHeightConstraint: 4779.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 10989.0 webViewHeightConstraint: 4779.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 10989.0 webViewHeightConstraint: 10989.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 25110.0 webViewHeightConstraint: 10989.0

scrollViewDidEndZooming webView.scrollView.contentSize.height: 25110.0 webViewHeightConstraint: 25110.0

我觉得你所做的一切都是即时的,这是问题所在,你最好添加一个全局高度数组,最初包含 table 单元格的默认高度,每次特定的 webview 是更改全局数组中与该单元格具有相同索引的相应值并重新加载 tableview ,将 tableViewCell 高度挂钩为 IBOutlet

并在 cellForRowAt

中执行此操作
 cell.heightConOfCell.constant = #coressponding value in global array#

不要忘记将这些放在 return 单元格

之前的行中
 cell.layoutSubviews()

 cell.layoutIfNeeded()

 return cell

根据您的问题,我预计您使用的是动态 tableViewCell 高度

我认为您为表格视图单元格提供了固定高度。

return 高度作为 UITableViewAutomaticDimension

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return UITableViewAutomaticDimension
}

试试改用这个:

func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat) {
    self.webHeightConstraint.constant = self.webHeightConstraint.constant * scale

    myDelegate?.reloadTableView()
}

这避免了必须引用 webViews 滚动视图的 contentSize,因为在缩小时它不会低于 webView 大小。

现在它不能完美地工作,因为你看到 webView 放大到自身,然后当你完成时它捕捉到新的大小,但它总是会像这样,因为在缩放完成之前没有任何更新。

另外不要忘记适当设置滚动视图的最小缩放和最大缩放级别。