mercredi 22 juin 2016

UITableView reloadRowsAtIndexPaths continuously reloads cell

I have a UITableView with a custom cell that contains a UIWebView. I am attempting to reload the cell with the correct height once the webview finishes loading the html content.

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
}

In the webViewDidFinishLoad delegate method, I attempt to reload the cell containing the webview.

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

When I run the app, the cell containing the webview continuously reload. The height seems to be correct and as desired but you can see the cell reloading non-stop. I can't seem to figure out why it continuously reloads. The HTML content I am loading is simple HTML and does not have multiple/endless frames so that webViewDidFinishLoad continuously gets called. I've also tried re-calculating and setting the height of the cell in webViewDidLoad but the height does not seem to be calculated correctly.

Aucun commentaire:

Enregistrer un commentaire