ios - 如何正确设置 uicollectionviewcells?

标签 ios swift cocoa-touch uicollectionview nsattributedstring

我有 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
}

}

问题是当我快速滚动时其他单元格也会变成链接。

What I want after feral scrolling

What I have

最佳答案

请在其他部分写一些东西。 喜欢:

 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/

相关文章:

ios - viewDidLoad 之外的闭包

iOS 8 企业应用程序更新

ios - 如何绘制一个矩形并根据电池电量填充颜色

ios - 如何使用 UINib 实例化和使用自定义 UITableViewCells

iphone - 何时在 iPhone 上使用 UIView 与 UIViewController?

ios - 处理 UI 中的异步事件

ios - 在 iOS 应用程序中打开提醒应用程序?

objective-c - iOS 相机 UIView 在 iPad 上覆盖全屏?

ios - 带有 UIImage 或远程 URL 的 UNNotificationAttachment

ios - 防止 UIView 在 Swift 中溢出到 SuperView 之外