【问题标题】:TableView glitches when loading an HTML content加载 HTML 内容时 TableView 出现故障
【发布时间】:2020-06-12 05:07:52
【问题描述】:

我正在标签上呈现我的 HTML 字符串。 另外我已经在 UIView 和 WebView 上渲染了它仍然是 tableview 故障

我应用了这个在标签上呈现 html 的函数。 这是代码。

func getAttributedString(HTMLString: String) -> NSAttributedString {
    let data = Data(HTMLString.utf8)
    let attributedString = try? NSAttributedString(data: data, options: [.documentType: NSAttributedString.DocumentType.html], documentAttributes: nil)
    return attributedString!
}

并在 cellForRowAt 上使用如下

 discussionListCell.discriptionLabel.attributedText = self.getAttributedString(HTMLString: discussionResponse[indexPath.row].postMsg ?? "")

她是我收到的带有图像的 HTML 字符串示例。 “他 img src=\"http:\\45.35.4.250\DevImageUpload\60ac0c0f-7751-4088-9a01-466a51a8a5b7.png\">"

在此先感谢。

【问题讨论】:

    标签: ios swift uitableview html-parsing


    【解决方案1】:

    使用 Dispatch 更新 UILabel

    DispatchQueue.main.async {
          self.discussionListCell.discriptionLabel.attributedText = self.getAttributedString(HTMLString: discussionResponse[indexPath.row].postMsg ?? "")
      }
    

    尝试像下面这样重新加载您的 tableView:

    DispatchQueue.main.async {
              self.tableView.reloadData()
          }
    

    【讨论】:

    • 试过你的解决方案,但现在我面临着表格视图单元格中数据错位的奇怪问题。现在有时它会在多个单元格中显示相似的数据。
    • 上述解决方案工作正常,您可能没有正确处理 tableview。请分享更多代码。我已经更新了上面的答案。请检查!
    • 我已经创建了单元格的对象,我唯一要做的就是将 html 解析为单个标签并返回单元格 这是代码let discussionListCell = tableView.dequeueReusableCell(withIdentifier: "DiscussionListCountyTableViewCell", for: indexPath) as! DiscussionListCountyTableViewCell DispatchQueue.main.async { discussionListCell.discriptionLabel.attributedText = self.getAttributedString(HTMLString: self.discussionResponse[indexPath.row].postMsg ?? "") } return discussionListCell
    • 是的,我也重新加载了 tableView 相同
    • 我尝试了上面的解决方案,但还是有问题。
    猜你喜欢
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    相关资源
    最近更新 更多