我有一個UITableView與包含UIWebView的自定義單元格。一旦webview完成加載html內容,我試圖用正確的高度重新加載單元格。UITableView reloadRowsAtIndexPaths不斷重新加載單元格
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)
}
當我運行應用程序時,包含webview的單元格不斷重新加載。高度似乎是正確的,而且你可以看到單元重新加載不停。我似乎無法弄清楚爲什麼它不斷重新加載。我正在加載的HTML內容是簡單的HTML,並且沒有多個/無限幀,因此webViewDidFinishLoad會不斷被調用。我也嘗試重新計算並設置webViewDidLoad中單元格的高度,但高度似乎沒有正確計算。