在UITableViewCell中更新UITextView的attributedText时,UITableView的滚动不平滑

问题描述 投票:0回答:1

我有一个具有UITableView的视图控制器,而tableview具有许多带有UITextView的单元格。

class FeatureCell: UITableViewCell {
    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var featureNameTextView: UITextView!
}

class ReviewViewController: UIViewController, UITableViewDataSource {
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let entity = tableViewData[indexPath.row]

        let cell: FeatureCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) as! FeatureCell

        let featureNameAttributedString = "<p>Actual HTML String</p>".getAttributedTextOfHTMLString(WithParagraphStyle: nil, font: Theme.textStyle.sfProTextRegular, textColor: .darkGray)
        cell.featureNameTextView.attributedText = featureNameAttributedString
        return cell
    }
}

featureNameAttributedString是由来自API的HTML字符串生成的。我已使用以下扩展名从HTML生成属性字符串。


    func getAttributedTextOfHTMLString(WithParagraphStyle paragraphStyle: NSMutableParagraphStyle?, font: UIFont?, textColor: UIColor?) -> NSMutableAttributedString? {
        guard let htmlData = NSString(string: self).data(using: String.Encoding.unicode.rawValue) else {
            return NSMutableAttributedString(string: self)
        }
        let options = [NSAttributedString.DocumentReadingOptionKey.documentType: NSAttributedString.DocumentType.html]
        guard let attributedString = try? NSMutableAttributedString(data: htmlData, options: options, documentAttributes: nil) else {
            return NSMutableAttributedString(string: self)
        }
        let finalAttributedText = NSMutableAttributedString(attributedString: attributedString)
        let range = NSRange(location: 0, length: attributedString.length)
        if let paragraph = paragraphStyle {
            finalAttributedText.addAttribute(.paragraphStyle, value: paragraph, range: range)
        }
        if let font = font {
            finalAttributedText.addAttribute(.font, value: font, range: range)
        }
        if let color = textColor {
            finalAttributedText.addAttribute(.foregroundColor, value: color, range: range)
        }
        return finalAttributedText
    }
}

如果featureNameAttributedString的长度过长,并且单元格的高度是根据attributedString的大小动态设置的,则滚动时tableView会很滞后。

如果实际的html字符串包含在sup标签中,并且单元格的高度是根据attributedString的大小动态设置的,则表视图将无法平滑滚动。

enter image description here

SécuritéMcAfeeMD-Mieux

如果将简单字符串文本而不是attributedText设置为textview,则滚动是平滑的。

有解决这个问题的主意吗?

ios swift uitableview scroll uitextview
1个回答
0
投票

异步加载它们。

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let entity = tableViewData[indexPath.row]

    let cell: FeatureCell = tableView.dequeueReusableCellWithIdentifier(CellIdentifier, forIndexPath: indexPath) as! FeatureCell
    DispatchQueue.main.async {
        let featureNameAttributedString = "<p>Actual HTML String</p>".getAttributedTextOfHTMLString(WithParagraphStyle: nil, font: Theme.textStyle.sfProTextRegular, textColor: .darkGray)
        cell.featureNameTextView.attributedText = featureNameAttributedString
    }
    return cell
}
© www.soinside.com 2019 - 2024. All rights reserved.