【问题标题】:Reading and writing an array in swift cause crash快速读取和写入数组导致崩溃
【发布时间】:2019-10-09 07:20:38
【问题描述】:

我正在我的tableview 中实现wkwebviews,我计算tableview 的高度将它存储在数组中并重新加载特定的单元格。 但是我的 heightForRowAt indexPath 导致崩溃。

var contentHeights : [CGFloat] = [400.0, 0.0, 0.0, 0.0,0.0]

我有 5 个单元格,定义了第一个单元格的高度,其余 4 个单元格由 wkwebview 组成。

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        DispatchQueue.main.asyncAfter(deadline: .now() + .milliseconds(100)) {

            if webView.tag < self.contentHeights.count{
                let currentContentHeight = webView.scrollView.contentSize.height
                if (self.contentHeights[webView.tag] < 50.0 && self.contentHeights[webView.tag] < currentContentHeight) {
                    self.contentHeights[webView.tag] = webView.scrollView.contentSize.height
                    webView.frame = CGRect(x: webView.frame.origin.x, y: webView.frame.origin.y, width: webView.frame.width, height: currentContentHeight)
                    self.uiViewController?.reloadTableViewrow(atIndexPath: webView.tag)
                }
            }
        }
    }

func reloadTableViewrow(atIndexPath:Int){
        jobDetailTableview.reloadRows(at: [IndexPath(row: atIndexPath, section: 0)], with: .none)

    }

我在 cellForRowAt indexPath 中分配 webview 标签

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    if indexPath.row == 0 {
        let cell = tableView.dequeueReusableCell(withIdentifier: “X1”, for: indexPath) as? X1
        if cell == nil {
            LogError.logErrorRequest(message: "nil jobInfo cell for index =  \(indexPath.row) for title = \(jobWebviewDetails[indexPath.row].name)", functionName: #function, className: "JobDetailDelegateAndDataSource", lineNumber: "\(#line)")
        }
        return cell!
    } else {
        let cell = tableView.dequeueReusableCell(withIdentifier: X2, for: indexPath)  as? X2
        cell?.selectionStyle = .none
        cell?.webview.tag = indexPath.row
        if cell == nil {
            LogError.logErrorRequest(message: "nil jobDetails cell for index =  \(indexPath.row) for title = \(jobWebviewDetails[indexPath.row].name)", functionName: #function, className: "JobDetailDelegateAndDataSource", lineNumber: "\(#line)")
        }
        return cell!
    }
}

但我在 HeightForRowAtIndexPath 函数中收到崩溃日志 我尝试放置日志但仍然没有成功

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    if indexPath.row == 0 {
        return 340.0
    } else {
        if indexPath.row < contentHeights.count {
            return (contentHeights[indexPath.row] + 70)
        } else {
// I am getting this logs, Its running fine on 90% on devices but getting this logs for few devices
            LogError.logErrorRequest(message: "Index path = \(indexPath.row) ", functionName: #function, className: "x1", lineNumber: "\(#line)")
            return 200
        }
    }
}

我认为因为我同时设置和获取数组可能是这个问题的原因。 如何解决这个问题

【问题讨论】:

  • 你要告诉我们崩溃说什么吗?

标签: ios uitableview swift4 heightforrowatindexpath


【解决方案1】:

暂时忘记崩溃,我个人认为这不是实现您的要求的好方法。

让单元格自己处理高度怎么样?

这个想法是,

  1. 创建自定义UITableViewCell,符合WKNavigationDelegate
  2. 在单元格中,实现func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!),计算单元格的高度并正确更新webView的约束。
  3. 在单元格中,计算完成后,调用viewController(作为其委托)使用tableView.beginUpdate()tableView.endUpdate()更新tableView
  4. 从 View Controller 中,当我们创建单元格时,让它知道它是否是第一个单元格,以便它始终保持其高度固定。

就是这样,无需保留任何数组,不再崩溃!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多