【发布时间】: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