Ios MessageKit集合视图没有';添加对自定义单元格的支持后,不显示任何单元格`
我已经阅读了有关在MessageKit中创建自定义单元格的指南,以及类似的问题 我正在尝试创建一个自定义单元格;以下是我从Ios MessageKit集合视图没有';添加对自定义单元格的支持后,不显示任何单元格`,ios,swift,uicollectionview,uicollectionviewlayout,messagekit,Ios,Swift,Uicollectionview,Uicollectionviewlayout,Messagekit,我已经阅读了有关在MessageKit中创建自定义单元格的指南,以及类似的问题 我正在尝试创建一个自定义单元格;以下是我从UICollectionViewCell继承的单元格代码: import UIKit import MessageKit open class ChatReferenceCell: UICollectionViewCell { @IBOutlet weak var authorLabel: UILabel! @IBOutlet weak var r
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文件:
在myChatViewController
中,我将聊天视图设置为:
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文件,我尝试更正了注册单元格的方式,但仍然得到了相同的结果:
messagescolectionview.register(UINib(nibName:“ChatReferenceCell”,bundle:nil),forCellWithReuseIdentifier:“kChatReferenceCollectionViewCell”)
试试这种方法
messagesCollectionView = MessagesCollectionView(frame: .zero, collectionViewLayout: MyCustomMessagesFlowLayout()
messagesCollectionView.register(ChatReferenceCell.self)
super.viewDidLoad()