我对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
。