【问题标题】:MessageKit Collection View doesn't display any cells after adding support for a custom cell`添加对自定义单元格的支持后,MessageKit 集合视图不显示任何单元格
【发布时间】:2021-07-28 00:08:17
【问题描述】:

我已经阅读了在 MessageKit here 中创建自定义单元格的指南,以及像 this 这样的问题

我正在尝试创建一个自定义单元格;这是我从 UICollectionViewCell 继承的单元格的代码:

import UIKit
import MessageKit

open class ChatReferenceCell: UICollectionViewCell {
    
    @IBOutlet weak var authorLabel: UILabel!
    @IBOutlet weak var referenceText: UITextView!
    @IBOutlet weak var participantsLabel: UILabel!
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubviews()
    }
    
    public required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setupSubviews()
    }
    
    open func setupSubviews() {
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
    }
    
    open func configure(with message: MessageType, at indexPath: IndexPath, and messagesCollectionView: MessagesCollectionView) {
        // Do stuff
        switch message.kind {
        case .custom(let data):
            guard let systemMessage = data as? String else { return }
            referenceText.text = systemMessage
        default:
            break
        }
    }
    
}

这是自定义单元格的 XIB 文件:

在我的ChatViewController 中,我这样设置聊天视图:

    messagesCollectionView.register(ChatReferenceCell.self)
    messagesCollectionView = MessagesCollectionView(frame: .zero, collectionViewLayout: MyCustomMessagesFlowLayout())
    messagesCollectionView.messagesDataSource = self
    messagesCollectionView.messagesLayoutDelegate = self
    messagesCollectionView.messagesDisplayDelegate = self
    messagesCollectionView.messageCellDelegate = self

以及配置自定义布局:

open class CustomMessageSizeCalculator: MessageSizeCalculator {
    open override func messageContainerSize(for message: MessageType) -> CGSize {
    // Customize this function implementation to size your content appropriately. This example simply returns a constant size
    // Refer to the default MessageKit cell implementations, and the Example App to see how to size a custom cell dynamically
        return CGSize(width: 300, height: 130)
    }
}


open class MyCustomMessagesFlowLayout: MessagesCollectionViewFlowLayout {
    lazy open var customMessageSizeCalculator = CustomMessageSizeCalculator(layout: self)

    override open func cellSizeCalculatorForItem(at indexPath: IndexPath) -> CellSizeCalculator {
        let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
        if case .custom = message.kind {
            return customMessageSizeCalculator
        }
        return super.cellSizeCalculatorForItem(at: indexPath);
    }
}

在添加自定义单元格之前,我能够添加文本单元格并在聊天视图控制器中按预期显示它们:

在尝试添加对自定义单元格的支持后,我得到了这样的布局:

编辑我尝试更正注册单元格的方式,因为我使用的是 NIB 文件,但仍然得到相同的结果:

messagesCollectionView.register(UINib(nibName: "ChatReferenceCell", bundle: nil), forCellWithReuseIdentifier: "kChatReferenceCollectionViewCell")

【问题讨论】:

  • 您是否找到任何解决方案,如果是,请发布您所做的更改?
  • 我想提一提的是,initWithCoder 实际上是被调用的。所以细胞是从笔尖加载的。但是它的高度不对,好像只有几个点……
  • 我通过覆盖CellSizeCalculator 类的sizeForItem 方法解决了这个问题。您只需要计算单元格的大小。

标签: ios swift uicollectionview uicollectionviewlayout messagekit


【解决方案1】:

试试这个方法。

messagesCollectionView = MessagesCollectionView(frame: .zero, collectionViewLayout: MyCustomMessagesFlowLayout()
messagesCollectionView.register(ChatReferenceCell.self)
super.viewDidLoad()

.....

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多