我有一个 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/