简体   繁体   English

添加对自定义单元格的支持后,MessageKit 集合视图不显示任何单元格

[英]MessageKit Collection View doesn't display any cells after adding support for a custom cell`

I've read through the guide for creating a custom cell in MessageKit here , and SO questions like this我已经阅读了在MessageKit中创建自定义单元格的指南,以及类似这样的问题

I'm trying to create a custom cell;我正在尝试创建一个自定义单元格; here's my code for a cell that inherits from a UICollectionViewCell :这是我从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
        }
    }
    
}

This is the XIB file for the custom cell:这是自定义单元格的 XIB 文件:

在此处输入图像描述

In my ChatViewController , I set up the chat view like so:在我的ChatViewController中,我像这样设置聊天视图:

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

As well as configure the custom layout:以及配置自定义布局:

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

Prior to adding in a custom cell, I was able to add text cells and display them as expected in the chat view controller:在添加自定义单元格之前,我能够添加文本单元格并在聊天视图 controller 中按预期显示它们:

在此处输入图像描述

After trying to add in support for a custom cell, I get a layout like this:在尝试添加对自定义单元格的支持后,我得到了这样的布局:

在此处输入图像描述

EDIT I tried correcting the way I register the cell since I'm using a NIB file, but still getting the same results:编辑我尝试更正注册单元格的方式,因为我使用的是 NIB 文件,但仍然得到相同的结果:

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

try this way.试试这个方法。

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

..... ......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM