Issue registering a custom message cell

Hey there!

I’m using UIKit version 2.1.4 and I’m having a bit of an issue trying to display my own custom cell for a custom message type.

From my understanding reading the docs, if I call the register(customMessageCell:) function, then any message sent to the chat with a customType should be displayed using my custom cell. But in practice my custom cell is being ignored, and custom messages are still being displayed as a regular user message.

I’ve attached some sample code below. Is this a bug in the UIKit SDK? Or is it possible that I’ve missed a step?

fileprivate class ChatViewController: SBUChannelViewController {
    
    override init(channel: SBDGroupChannel, messageListParams: SBDMessageListParams? = nil) {
        super.init(channel: channel, messageListParams: messageListParams)
        hidesBottomBarWhenPushed = true
    }
    
    private lazy var customButton = UIBarButtonItem(title: "Custom", style: .plain, target: self, action: #selector(sendCustomMessage))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.rightBarButtonItem = customButton
        register(customMessageCell: CustomCell())
    }
    
    @objc private func sendCustomMessage() {
        guard let params = SBDUserMessageParams(message: "This is a custom message") else { return }
        params.customType = "custom_message"
        params.data = "custom_message_data"
        sendUserMessage(messageParams: params)
    }
    
}

fileprivate class CustomCell: SBUBaseMessageCell {
    
    override func configure(message: SBDBaseMessage, position: MessagePosition, hideDateView: Bool, groupPosition: MessageGroupPosition = .none, receiptState: SBUMessageReceiptState) {
        super.configure(message: message, position: position, hideDateView: hideDateView, groupPosition: groupPosition, receiptState: receiptState)
        contentView.backgroundColor = .systemPink
    }
    
}

Hi @benzgrant,

The customMessageCell is used for you to add additional custom message cell, apart from our default userMessageCell, fileMessageCell, adminMessageCell and unknownMessageCell.
Once the customMessageCell is registered by register(customMessageCell:, nib:), it’s registered with cellReuseIdentifier
as customMessageCell.sbu_className, so you’ll need to retrieve the cell from tableView(_ tableView:, cellForRowAt indexPath:) using the same identifier, and configure your custom cell.
If you wish to register it with other reuseIdentifier, you can do it so by registering it directly using the tableView property.

We’ll fix the docs as this customMessageCell has nothing to do with the message’s custom type.

Thanks for the reply @hoons.

I did a bit more digging and I actually stumbled across a much easier way to implement custom cells, by overriding the generateCellIdentifier(by message:) function in SBUChannelViewController. I can’t find a reference to this function anywhere in the documentation but it does exactly what I need.

Here’s an updated example of my code in case anyone is trying to achieve the same thing as me:

class ChatViewController: SBUChannelViewController {
        
    private lazy var customButton = UIBarButtonItem(title: "Custom", style: .plain, target: self, action: #selector(sendCustomMessage))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.rightBarButtonItem = customButton
        tableView.register(CustomMessageCell.self, forCellReuseIdentifier: "CustomMessageCellReuseIdentifier")
    }
    
    @objc private func sendCustomMessage() {
        guard let params = SBDUserMessageParams(message: "") else { return }
        params.customType = "my_custom_type"
        sendUserMessage(messageParams: params)
    }
    
    override func generateCellIdentifier(by message: SBDBaseMessage) -> String {
        if message.customType == "my_custom_type" {
            return "CustomMessageCellReuseIdentifier"
        }
        return super.generateCellIdentifier(by: message)
    }
    
}

class CustomMessageCell: SBUBaseMessageCell {
        
    override func configure(message: SBDBaseMessage, position: MessagePosition, hideDateView: Bool, groupPosition: MessageGroupPosition = .none, receiptState: SBUMessageReceiptState) {
        super.configure(message: message, position: position, hideDateView: hideDateView, groupPosition: groupPosition, receiptState: receiptState)
        contentView.backgroundColor = .systemPink
    }
    
}

Hi @benzgrant

Yes you can do it that way as well.
I will make sure to add the generateCellIdentifier(by message:) to the docs.

Thank you.