我有一个可扩展的UITableView,它通过父单元格的插入/删除调用添加/删除子UITableViewCells。每个子单元格都包含一个WKWebView对象,我用它来渲染一些本地HTML字符串,并在加载字符串后根据内容高度调整单元格的大小。
如果我通过一次点击一个父单元格来缓慢扩展tableview,一切正常。但是,如果我尝试一次加载多个子单元格,或者快速加载同一个单元格(例如,通过快速连续点击几次父单元格),webview将无法加载内容并显示为空白。
我有一种感觉,因为我想在之前的加载完成之前再次调用WKWebView的didFinishNavigation
。有没有办法阻止它在加载之前重新调用它(即在CellForRowAt
方法中同步加载单元格?另外的建议也欢迎。下面的相关代码。
另外值得注意的是 - 我通过委托将SectionContentTableViewCell(孩子)的内容高度发送到视图控制器,然后我可以调用heightForRowAt
。高度存储在结构中以加速后续重新加载。
class SectionContentTableViewCell: UITableViewCell, WKNavigationDelegate {
var webView: WKWebView?
var htmlString = ""
var contentSection: Int = 0
weak var contentDelegate: ContentCellDelegate? = nil
override func layoutSubviews() {
super.layoutSubviews()
webView?.navigationDelegate = self
webView?.autoresizingMask = .flexibleWidth
}
func configureWebView() {
webView?.loadHTMLString(htmlString, baseURL: URL(fileURLWithPath: Bundle.main.path(forResource: "cases", ofType: "css")!))
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
self.perform(#selector(webViewLoadCompleted), with: nil, afterDelay: 0.1)
}
func webViewLoadCompleted() {
let cellHeight: CGFloat = (webView?.scrollView.contentSize.height)!
print("cellHeight \(String(describing: cellHeight))")
webView?.frame.size.height = cellHeight
contentDelegate?.sendContentCellHeightData(cellHeight: cellHeight, section: contentSection)
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
switch visibleQuestionRowsArray[indexPath.row] {
//Value 0 = section cell
case 0:
let sectionCell = tableView.dequeueReusableCell(withIdentifier: "sectionCell", for: indexPath) as! ShowHideSectionTableViewCell
let sectionIndex = determineTrueIndexForSectionCellWith(cellIndex: indexPath.row)
sectionCell.sectionLabel.text = "Section \(sectionIndex)"
if indexPath.row+1 == visibleQuestionRowsArray.count {
sectionCell.expandIndicatorLabel.text = "+"
} else if visibleQuestionRowsArray[indexPath.row+1] == 0 {
sectionCell.expandIndicatorLabel.text = "+"
} else {
sectionCell.expandIndicatorLabel.text = "-"
}
return sectionCell
//Value 1 = content cell
case 1:
let contentCell = tableView.dequeueReusableCell(withIdentifier: "contentCell", for: indexPath) as! SectionContentTableViewCell
let parentIndex = determineParentForContentCellWith(cellIndex: indexPath.row, currentSegment: currentSegment)
contentCell.contentDelegate = self
contentCell.contentSection = parentIndex
contentCell.htmlString = questionHTMLStringsArray[parentIndex]
//Check if height already recorded (therefore webview already instantiated)
let checkOpen = contentHeights.filter({$0.segment == currentSegment && $0.section == parentIndex && $0.width == currentViewWidth})
if checkOpen.isEmpty == true {
print("webView instantiated")
contentCell.webView = WKWebView(frame: CGRect(x: 0, y: 0, width: contentCell.frame.width, height: 44.0), configuration: WKWebViewConfiguration())
contentCell.contentView.addSubview(contentCell.webView!)
}
contentCell.configureWebView()
return contentCell
default:
return UITableViewCell()
}