2

我有一个关于 MessageKit 的问题。我们如何指定特定的聊天单元格,以便在单击时didTapAccessoryView(in cell:)获得该附件视图的索引。在显示新的 VC 之前,我需要索引来注入一些信息。有可能还是我在浪费时间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)
    }
}

我尝试将目标添加到按钮,如下所示:

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

按钮不是交互式的,也不didTapAccessoryView(in cell: MessageCollectionViewCell)是我这样做时调用的button.isUserInteractionEnabled = true

4

0 回答 0