Skip to content
This repository has been archived by the owner on Aug 14, 2019. It is now read-only.

Easier way to handle custom menu actions #810

Closed
sebastianludwig opened this issue Feb 21, 2015 · 7 comments
Closed

Easier way to handle custom menu actions #810

sebastianludwig opened this issue Feb 21, 2015 · 7 comments
Milestone

Comments

@sebastianludwig
Copy link
Collaborator

Hi,

I've tried to add a Delete action to the cell menu. I read #393 #483 and #569. As suggested I added a category on JSQMessagesCollectionViewCell to implement delete: and in the collection view delegate return YES for @selector(delete:) in collectionView:canPerformAction:forItemAtIndexPath:withSender:.

But collectionView:performAction:forItemAtIndexPath:withSender: is only called for actions like copy: or cut:. A look at the disassembly revealed that the the cell holds an internal reference to the collection view and dispatches the messages to the delegate this way.

This is difficult to achieve with JSQ because the only way to add references through categories is using associated objects or subclassing which brings it's own problems and seems overkill.

I think it'd be cleanest to extend the JSQMessagesCollectionViewCellDelegate with a method messagesCollectionViewCell:performAction:withSender: (maybe optional). This would make it easy and straight forward to implement custom actions and perform the actual task where it belongs (not in the cell).

The only way to make it even easier would be to add an accessible reference to the collection view and dispatch to the UICollectionViewDelegate all the time (like Apple does).

What do you think? If you agree I'm happy to implement it and send a pull request.

@jessesquires
Copy link
Owner

Thanks @sebastianludwig 👍 I agree, the API around this is clunky and makes me 😢

Your suggestion sounds great — I'd love to see it.

I hate @optional protocol methods, so if it's possible to avoid it then that would be even better.

sebastianludwig added a commit to sebastianludwig/JSQMessagesViewController that referenced this issue Feb 21, 2015
@gblazex
Copy link

gblazex commented Mar 17, 2015

+1, I have to check this out. The new versions of JSQ, while awesome overall, made custom message actions a nightmare.

@jessesquires
Copy link
Owner

@galambalazs - PR #811 adds this functionality and I'm planning on merging it for the next release.

@gblazex
Copy link

gblazex commented Mar 17, 2015

@jessesquires Wow, awesome man. Thanks :)

@gblazex
Copy link

gblazex commented Mar 19, 2015

4000 stars to celebrate 7.0.0
nice 👍

@jessesquires jessesquires added this to the Release 7.1.0 milestone May 6, 2015
@basawarajh-cuelogic
Copy link

Hi,
How to handle delete action in JSQMessageController, I have implemented method

    override func collectionView(collectionView: JSQMessagesCollectionView!, didDeleteMessageAtIndexPath indexPath: NSIndexPath!) {
    self.collectionView?.deleteItemsAtIndexPaths([indexPath])
}

Also override method

    override func collectionView(collectionView: UICollectionView, shouldShowMenuForItemAtIndexPath indexPath: NSIndexPath) -> Bool {
    // Do the custom JSQM stuff
    super.collectionView(collectionView, shouldShowMenuForItemAtIndexPath: indexPath)
    // And return true for all message types (we don't want the long press menu disabled for any message types)
    return true
}

  override func collectionView(collectionView: UICollectionView, canPerformAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) -> Bool {
    super.collectionView(collectionView, canPerformAction: action, forItemAtIndexPath: indexPath, withSender: sender)
    return true
}

override func collectionView(collectionView: UICollectionView, performAction action: Selector, forItemAtIndexPath indexPath: NSIndexPath, withSender sender: AnyObject?) {
    super.collectionView(collectionView, performAction: action, forItemAtIndexPath: indexPath, withSender: sender)
}

but i get crash on menu item what else to be done to work, any help would be appreciated, thanks in advance.

@sebastianludwig
Copy link
Collaborator Author

What does the exception message say? Where and when exactly does the app crash?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants