为什么HTML转NSAttributeString会导致UITableView内部不一致?

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

在 UITableView 数据源中,我将 HTML 字符串转换为 NSAttributedString 并使用以下例程将其设置在单元格中的标签上,

        let attrStr = try! NSAttributedString(
            data: modifiedFont.data(using: .utf8, allowLossyConversion: false)!,
            options: [.documentType: NSAttributedString.DocumentType.html,
                      .characterEncoding:String.Encoding.utf8.rawValue],
            documentAttributes: nil)

这有时会导致 UITableView 变得不稳定。在控制台中生成错误,

Error is "Assert UITableView internal inconsistency prefetchedCells and indexPathsForPrefetchedCells are out of sync
.

有时这表现为重复的行,部分绘制的行,索引超出范围而崩溃。

Stackoverflow

中发现了类似的问题

那里提出的解决方案有效。我将从 HTML 到 NSAttributedString 的转换移到了更早的步骤,我正在为 tableview 创建数据并将其移出

cellForRowAt..
数据源方法。

为什么这是一个修复?为什么这种转换会在填写 UITableViewCell 时导致问题?

ios uitableview crash
© www.soinside.com 2019 - 2024. All rights reserved.