2

I have a question on MessageKit. How could we specified particular chat cell so that when we click on didTapAccessoryView(in cell:) we get index of that accessoryView. I need index to inject some information prior to showing new VC. Is it even possible or am I wasting my time with MessageCellDelegate.

// MARK: - MessageCellDelegate
extension ChatVC: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) {
        let chatChallengeVC = DIConfigurator.sharedInst().getChatChallengeVC() 
        self.navigationController?.show(chatChallengeVC, sender: nil)
    }
}

I have tried adding target to button like as follow:

    func configureAccessoryView(_ accessoryView: UIView, for message: MessageType, at indexPath: IndexPath, in messagesCollectionView: MessagesCollectionView) {
        // Cells are reused, so only add a button here once. For real use you would need to
        // ensure any subviews are removed if not needed
        switch(message.kind){
            case .photo(_):
                guard accessoryView.subviews.isEmpty else { return }
                accessoryView.isUserInteractionEnabled = true
                accessoryView.target(forAction: #selector(tapped), withSender: nil)
                let button = UIButton(type: .infoLight)
                button.tintColor = .primaryChatColor
                accessoryView.addSubview(button)
                button.frame = accessoryView.bounds
                button.isUserInteractionEnabled = true

                // Like this
                button.target(forAction: #selector(tapped(sender:)), withSender: self)
                accessoryView.layer.cornerRadius = accessoryView.frame.height / 2
                accessoryView.backgroundColor = UIColor.primaryChatColor.withAlphaComponent(0.3)
            default:
                log.info("Default value")
        }
    }

Button is not interactive nor didTapAccessoryView(in cell: MessageCollectionViewCell) is called when I do button.isUserInteractionEnabled = true.

user256419
  • 81
  • 2
  • 10

1 Answers1

0

make accessoryView.isUserInteractionEnabled = false and button.isUserInteractionEnabled = false then didTapAccessoryView(in cell: MessageCollectionViewCell) gets called, it works for me.