我有uicollectionview作为聊天视图,而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
}
}
问题是当我快速滚动时,其他单元格也变为链接。
答案 0 :(得分:0)
请在其他部分写一些东西。 喜欢:
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
}
希望它会为您提供帮助。谢谢。
答案 1 :(得分:0)
如果不是链接,则设置UITextView的text属性。
fileprivate func setupCell(cell: ChatMessageCell, message: RequestMessage) {
cell.textView.attributedText = nil
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
}
else{
cell.textView.text = message.messageText
cell.textView.delegate = self
}
答案 2 :(得分:0)
通过设置没有链接字段的相同属性解决了属性问题。如果您知道更清洁的决定,请写下来。
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
} else {
cell.textView.attributedText = NSMutableAttributedString(string: message.messageText, attributes:[NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)])
}