【问题标题】:UITableView reloadRowsAtIndexPaths continuously reloads cellUITableView reloadRowsAtIndexPaths 不断地重新加载单元格
【发布时间】: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


    【解决方案1】:

    当 webViewDidFinishLoad 你可以通过 webview.scrollView.contentSize.height 获得高度,然后调用 reloadRowsAtIndexPaths

    【讨论】:

      【解决方案2】:

      当然它会不断地重新加载。这就是你编写它的目的。

      每次加载单元格时,都会触发单元格内的 Web 视图进行加载。 加载完成后,它会调用webViewDidFinishLoad,您可以在其中触发同一单元格的重新加载。然后它调用cellForRowAtIndexPath,这会触发 Web 视图再次加载。等等。

      您不需要在您的webViewDidFinishLoad 中调用reloadRowsAtIndexPaths。 Web 视图应该会自动更新其内容,而无需您执行任何操作。

      【讨论】:

        猜你喜欢
        • 2021-03-21
        • 2011-07-27
        • 1970-01-01
        • 1970-01-01
        • 2011-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多