ios - UITableView reloadRowsAtIndexPaths 不断重新加载单元格

标签 ios swift uitableview uiwebview

我有一个 UITableView,其中包含一个包含 UIWebView 的自定义单元格。一旦 webview 完成加载 html 内容,我就会尝试以正确的高度重新加载单元格。

override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
    height =  tableView.frame.size.height - tableView.contentSize.height
    return height
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    var cell:UITableViewCell!
    cell = tableView.dequeueReusableCellWithIdentifier("bodyFieldCellIdentifier", forIndexPath: indexPath)
    (cell as! SMBodyTableViewCell).frame = CGRect(x: 0, y: 0, width: width, height: height)
    (cell as! SMBodyTableViewCell).bodyWebView.delegate = self
    (cell as! SMBodyTableViewCell).bodyWebView.loadHTMLString(self.messageBody, baseURL: nil)
    (cell as! SMBodyTableViewCell).bodyWebView.tag = indexPath.row
}

在 webViewDidFinishLoad 委托(delegate)方法中,我尝试重新加载包含 webview 的单元格。

func webViewDidFinishLoad(aWebView: UIWebView) {
    let indexPath = NSIndexPath(forRow: aWebView.tag, inSection: 0)
    self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}

当我运行应用程序时,包含 webview 的单元格不断重新加载。高度似乎是正确的并且符合预期,但您可以看到单元不停地重新加载。我似乎无法弄清楚为什么它不断重新加载。我正在加载的 HTML 内容是简单的 HTML,并且没有多个/无尽的框架,因此 webViewDidFinishLoad 会不断被调用。我还尝试重新计算和设置 webViewDidLoad 中单元格的高度,但高度似乎没有正确计算。

最佳答案

当 webViewDidFinishLoad 时,您可以通过 webview.scrollView.contentSize.height 获取高度,然后调用 reloadRowsAtIndexPaths

关于ios - UITableView reloadRowsAtIndexPaths 不断重新加载单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37934404/

相关文章:

ios - Realm 中的动态属性

ios - 如何在上一个选定行的 didDeselectRow 处知道下一个选定行的 IndexPath?

ios - 带有解析查询的 UITableView 未加载

ios - 使用搜索栏时 TableView 中的重复项

ios - Swift 和 HealthKit : type of expression too ambiguous without more context

ios - View 的自动布局隐藏 subview

swift - 如何在swift中获取数组的元素类型

swift - 以编程方式添加的 UIButton 在切换场景后不会消失

Swift appdelegate 错误 - appdelegate 未确认协议(protocol)

ios - 如何在 iOS 11 中隐藏章节标题?