ios - 在 Swift Messagekit 应用程序中未检测到点击事件?

标签 ios swift messagekit

我有一个使用 Messagekit 标准实现的应用程序,但是当我点击消息文本时 didTapMessage 事件没有触发(didTapAvatar 函数也根本不运行)。我没有使用自定义单元格,所以我认为我不需要使用自定义手势识别器。

相关代码如下:

import UIKit
import Firebase
import FirebaseFirestore
import MessageKit
import MessageInputBar

class ChatViewController: MessagesViewController {

    // MARK: Lifecycle
    override func viewDidLoad() {
        super.viewDidLoad()

        messageInputBar.delegate = self
        messagesCollectionView.messagesDataSource = self
        messagesCollectionView.messagesLayoutDelegate = self
        messagesCollectionView.messagesDisplayDelegate = self

        .....

        }

    // MARK: Properties

    ...
}

extension ChatViewController: MessagesDataSource {

    func currentSender() -> Sender {
        //guard let currentUserID = User.current?.key else {return nil}
        let newSender = Sender(id: (User.current?.key)!, displayName: (User.current?.username)!)
        return newSender
    }

    func numberOfSections(in messagesCollectionView: MessagesCollectionView) -> Int {
        //return 1
        return messages.count
    }

    func messageForItem(at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) -> MessageType {
        return messages[indexPath.section]

        func cellTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {

            return NSAttributedString(string: MessageKitDateFormatter.shared.string(from: message.sentDate), attributes: [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 10), NSAttributedString.Key.foregroundColor: UIColor.darkGray])
        }

        func messageTopLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {
            let name = message.sender.displayName
            return NSAttributedString(string: name, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption1)])
        }

        func messageBottomLabelAttributedText(for message: MessageType, at indexPath: IndexPath) -> NSAttributedString? {

            let dateString = formatter.string(from: message.sentDate)
            return NSAttributedString(string: dateString, attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .caption2)])
        }
    }
}

extension ChatViewController: MessagesDisplayDelegate, MessagesLayoutDelegate {}

extension ChatViewController: MessageCellDelegate {

    func didTapAvatar(in cell: MessageCollectionViewCell) {
        print("Avatar tapped")
    }

    func didTapMessage(in cell: MessageCollectionViewCell) {
        print("Message tapped")

    }

    func didTapCellTopLabel(in cell: MessageCollectionViewCell) {
        print("Top cell label tapped")
    }

    func didTapMessageTopLabel(in cell: MessageCollectionViewCell) {
        print("Top message label tapped")
    }

    func didTapMessageBottomLabel(in cell: MessageCollectionViewCell) {
        print("Bottom label tapped")
    }

    func didTapAccessoryView(in cell: MessageCollectionViewCell) {
        print("Accessory view tapped")
    }
}

extension ChatViewController: MessageInputBarDelegate {
    func messageInputBar(
        _ inputBar: MessageInputBar,
        didPressSendButtonWith text: String) {
        ....
    }
}

有什么想法吗?谢谢!

最佳答案

我猜你丢了这个:

messagesCollectionView.messageCellDelegate = self

完整的实现引用(来自 https://github.com/MessageKit/MessageKit ):

func configureMessageCollectionView() {

    messagesCollectionView.messagesDataSource = self
    messagesCollectionView.messageCellDelegate = self

    scrollsToBottomOnKeyboardBeginsEditing = true // default false
    maintainPositionOnKeyboardFrameChanged = true // default false

    messagesCollectionView.addSubview(refreshControl)
    refreshControl.addTarget(self, action: #selector(loadMoreMessages), for: .valueChanged)
}

关于ios - 在 Swift Messagekit 应用程序中未检测到点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54837617/

相关文章:

ios - 如果从Feed中删除对象,则从Core Data中删除对象

ios - Pan Gesture 正在工作,但 ONTapGesture 不在 UIView 上工作

iOS背景 Sprite 不显示

ios - UICollectionView 单元格阴影

android - 从 url 打开 pdf 文件时在 flutter 中添加进度条

ios - 根据所选字符串在 Collection View 中显示图像

swift - 获取 NSTableView 的第一个可见行

ios - 当用户使用 'MessageKit' 库点击发送时,监听器不会触发

ios - 如何使用 MessageKit 创建自定义单元格?

ios - 有没有办法可以更改消息套件中整个输入栏的背景颜色?