【问题标题】:How do I setup uicollectionviewcells properly?如何正确设置 uicollectionview 单元格?
【发布时间】:2018-12-27 15:25:14
【问题描述】:

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

}

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

What I want after feral scrolling

What I have

【问题讨论】:

    标签: ios swift cocoa-touch uicollectionview nsattributedstring


    【解决方案1】:

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

     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
    }
    

    希望对您有所帮助。谢谢。

    【讨论】:

    • 我试过添加这个attributedString.removeAttribute(.link, range: NSMakeRange(0, attributedString.length)),但不起作用
    【解决方案2】:

    如果不是链接,则设置 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
        }
    

    【讨论】:

    • 属性仍然移动到其他单元格
    • 当属性文本不是链接时,您是否尝试将其设置为零?
    • Close) 如果我(仅) .attributedText = nil 我没有链接的消息是空的。但是当我添加 .text 时,我遇到了同样的问题 - 属性无处不在。嗯,如何正确设置文本...
    • 在函数的开头设置属性文本为nil。
    • 不起作用,但我做到了else { cell.textView.attributedText = NSMutableAttributedString(string: message.messageText, attributes:[NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)]) }。没有链接)非常感谢
    【解决方案3】:

    属性的问题是通过设置没有链接字段的相同属性来解决的。如果您知道更清洁的决定,请写下来。

    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)])
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-22
      相关资源
      最近更新 更多