【发布时间】:2016-06-21 01:57:11
【问题描述】:
我有一个带有自定义单元格的 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 委托方法中,我尝试重新加载包含 webview 的单元格。
func webViewDidFinishLoad(aWebView: UIWebView) {
let indexPath = NSIndexPath(forRow: aWebView.tag, inSection: 0)
self.tableView.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
当我运行应用程序时,包含 web 视图的单元格会不断重新加载。高度似乎正确且符合要求,但您可以看到单元格不停地重新加载。我似乎无法弄清楚为什么它会不断重新加载。我正在加载的 HTML 内容是简单的 HTML,并且没有多个/无限帧,因此 webViewDidFinishLoad 会不断被调用。我也尝试在 webViewDidLoad 中重新计算和设置单元格的高度,但高度似乎计算不正确。
【问题讨论】:
标签: ios swift uitableview uiwebview