我有 uicollectionview 作为聊天 View ,uicollectionviewcell 是聊天消息。有些消息有链接,我的 collectionView(_:cellForItemAt:) 调用 setupCell(cell:message:) 函数
fileprivate func setupCell(cell: ChatMessageCell, message: RequestMessage) {
var attributedString = NSMutableAttributedString()
if let url = message.url {
attributedString = NSMutableAttributedString(string: message.messageText, attributes:[NSAttributedStringKey.link: url, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)])
cell.textView.attributedText = attributedString
cell.textView.delegate = self
}
}
问题是当我快速滚动时其他单元格也会变成链接。
最佳答案
请在其他部分写一些东西。 喜欢:
if let url = message.url {
attributedString = NSMutableAttributedString(string: message.messageText, attributes:[NSAttributedStringKey.link: url, NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)])
cell.textView.attributedText = attributedString
cell.textView.delegate = self
} else {
//Some code
}
希望对您有帮助。谢谢。
关于ios - 如何正确设置 uicollectionviewcells?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51423511/