I'm using MessageKit in my iOS project to create a custom chat interface and I've run into a problem when trying to return a custom CellSizeCalculator for my custom cell that displays a series of buttons.
I have followed the MessageKit documentation to set up my custom cell and size calculator. Despite this, I'm receiving a fatal error that says "Must return a CellSizeCalculator for MessageKind.custom(Any?)". This occurs even though I have provided a custom size calculator for the custom message kind.
Here's the relevant code snippet:
func cellForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> UICollectionViewCell {
let message = messageForItem(at: indexPath, in: messagesCollectionView)
switch message.kind {
case .custom:
// Dequeue and configure the first cell with buttons
guard let cell = messagesCollectionView.dequeueReusableCell(withReuseIdentifier: CustomerVCCell.reuseIdentifier, for: indexPath) as? CustomerVCCell else {
fatalError("Unable to dequeue FAQButtonCell.")
}
cell.configure(with: faqData, delegate: self, at: indexPath, and: messagesCollectionView)
// cell.configure(with: faqData, delegate: self)
return cell
case .text:
// Handle text messages for user and other user
let cellIdentifier = MessageContentCell.reuseIdentifier
guard let cell = messagesCollectionView.dequeueReusableCell(withReuseIdentifier: cellIdentifier, for: indexPath) as? MessageContentCell else {
fatalError("Unable to dequeue MessageContentCell.")
}
cell.configure(with: message, at: indexPath, and: messagesCollectionView)
return cell
default:
fatalError("Unknown message kind")
}
}
}
import MessageKit
class MyCustomMessagesFlowLayout: MessagesCollectionViewFlowLayout {
lazy open var customMessageSizeCalculator = FAQButtonCellSizeCalculator(layout: self)
override open func cellSizeCalculatorForItem(at indexPath: IndexPath) -> CellSizeCalculator {
if isSectionReservedForTypingIndicator(indexPath.section) {
return typingIndicatorSizeCalculator
}
let message = messagesDataSource.messageForItem(at: indexPath, in: messagesCollectionView)
if case .custom = message.kind {
return customMessageSizeCalculator
}
return super.cellSizeCalculatorForItem(at: indexPath);
}
}