2016-06-21 39 views
0

我有一個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中單元格的高度,但高度似乎沒有正確計算。

回答

0

時webViewDidFinishLoad您可以通過webview.scrollView.contentSize.height得到高度,然後調用reloadRowsAtIndexPaths

0

當然好它不斷地重新加載。這就是你寫它要做的。

每次加載單元格時,都會觸發加載單元格內的網頁視圖。 加載完成後,它會調用webViewDidFinishLoad,觸發重新加載同一單元格。然後它調用cellForRowAtIndexPath,這會觸發Web視圖加載AGAIN。並繼續。

您不應該在webViewDidFinishLoad需要致電reloadRowsAtIndexPaths。 Web視圖應該自動更新它的內容,而無需執行任何操作。

相關問題