我正在实现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
}
}
}
我认为因为同时设置和获取数组可能是导致此问题的原因。如何解决这个问题
暂时忘记崩溃,我个人认为这不是实现您的要求的好方法。
让细胞自己处理高度如何?
想法是,
UITableViewCell
,符合WKNavigationDelegate
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!)
,计算单元格的高度,并适当地更新webView
的约束。tableView.beginUpdate()
和tableView.endUpdate()
来更新tableView >>就是这样,不需要保留任何数组,没有更多的崩溃!