CollectionView 单元格上的 AttributedString 不显示字符串值

AttributedString on a CollectionView cell is not displaying string value

我有一个标签设置为 attributedString 的 UICollectinViewCell。

在 cellForItem 中,我将一个简单的字符串转换为 NSAttributedString,以便我可以设置 UILabel 的属性文本 属性。

字符串 "Attributed String Text" 仅在我滚动 CollectionView 时出现。如果我不滚动,"Label" 出现,因为这是在 IB 中设置的文本。如果我将 UILabel 保持原样,正确的文本就会显示得很好。

// cellForItemAt
let string = "Attributed String Text"
let mutableAttributedString = NSAttributedString(string: string)
cell.subName.attributedText = mutableAttributedString

如何在不滚动 CollectionView 的情况下显示属性文本?

我明白了。我相信这可能是一个 Xcode 错误。我的代码没有任何问题。普通(纯)文本 UILabel 可以很好地显示正确的文本。当我将它切换到属性 UILabel 时,显示的文本 stopped 没有其他代码更改。

经过一段时间的挖掘,我发现了这个帖子:Label text in UICollectionViewCell not updating 用户 Jacksonsox 也遇到了类似的问题。

在 Interface Builder 中,我删除了与未更新的 UILabel 的连接,删除了 UILabel,重新添加了一个新标签,重新连接它,将其设置为属性文本,现在文本显示了正好。同样,没有进行任何代码更改。只有 IB 有变化。

我希望这对遇到类似问题的其他人有所帮助。