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

Commit

Permalink
Merge pull request #811 from sebastianludwig/develop
Browse files Browse the repository at this point in the history
Implemented #810: An easier way to handle custom menu actions. close #810
  • Loading branch information
jessesquires committed May 6, 2015
2 parents 9db018b + b728ff3 commit c4d38a8
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
20 changes: 20 additions & 0 deletions JSQMessagesDemo/DemoMessagesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ - (void)viewDidLoad
{
[super viewDidLoad];

[JSQMessagesCollectionViewCell registerMenuAction:@selector(delete:)];

self.title = @"JSQMessages";

/**
Expand Down Expand Up @@ -502,7 +504,25 @@ - (UICollectionViewCell *)collectionView:(JSQMessagesCollectionView *)collection
return cell;
}

#pragma mark - UICollectionView Delegate

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if (action == @selector(delete:)) {
return YES;
}

return [super collectionView:collectionView canPerformAction:action forItemAtIndexPath:indexPath withSender:sender];
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
if (action == @selector(delete:)) {
NSLog(@"delegate action %@", NSStringFromSelector(action));
} else {
[super collectionView:collectionView performAction:action forItemAtIndexPath:indexPath withSender:sender];
}
}

#pragma mark - JSQMessages collection view flow layout delegate

Expand Down
13 changes: 13 additions & 0 deletions JSQMessagesViewController/Views/JSQMessagesCollectionView.m
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,17 @@ - (void)messagesCollectionViewCellDidTapCell:(JSQMessagesCollectionViewCell *)ce
touchLocation:position];
}

- (void)messagesCollectionViewCell:(JSQMessagesCollectionViewCell *)cell didPerformAction:(SEL)action withSender:(id)sender
{
NSIndexPath *indexPath = [self indexPathForCell:cell];
if (indexPath == nil) {
return;
}

[self.delegate collectionView:self
performAction:action
forItemAtIndexPath:indexPath
withSender:sender];
}

@end
21 changes: 21 additions & 0 deletions JSQMessagesViewController/Views/JSQMessagesCollectionViewCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@
*/
- (void)messagesCollectionViewCellDidTapCell:(JSQMessagesCollectionViewCell *)cell atPosition:(CGPoint)position;

/**
* Tells the delegate that an actions has been selected from the menu of this cell.
*
* @param cell The cell that displayed the menu.
* @param action The action that has been performed.
* @param sender The object that initiated the action.
*
* @discussion This method is automatically called for any registered actions.
*
* @see `JSQMessagesCollectionViewCell`
*/
- (void)messagesCollectionViewCell:(JSQMessagesCollectionViewCell *)cell didPerformAction:(SEL)action withSender:(id)sender;

@end


Expand Down Expand Up @@ -180,4 +193,12 @@
*/
+ (NSString *)mediaCellReuseIdentifier;

/**
* Registeres an action to be available in the cell's menu.
*
* @discussion Non-Standard actions still need to be added to the `UIMenuController`
* manually.
*/
+ (void)registerMenuAction:(SEL)action;

@end
46 changes: 46 additions & 0 deletions JSQMessagesViewController/Views/JSQMessagesCollectionViewCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,21 @@ - (void)jsq_updateConstraint:(NSLayoutConstraint *)constraint withConstant:(CGFl
@end


static NSMutableArray *jsqMessagesCollectionViewCellActions = nil;


@implementation JSQMessagesCollectionViewCell

#pragma mark - Class methods

+ (void)initialize
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
jsqMessagesCollectionViewCellActions = [[NSMutableArray alloc] init];
});
}

+ (UINib *)nib
{
return [UINib nibWithNibName:NSStringFromClass([self class]) bundle:[NSBundle bundleForClass:[self class]]];
Expand All @@ -86,6 +96,11 @@ + (NSString *)mediaCellReuseIdentifier
return [NSString stringWithFormat:@"%@_JSQMedia", NSStringFromClass([self class])];
}

+ (void)registerMenuAction:(SEL)action
{
[jsqMessagesCollectionViewCellActions addObject:NSStringFromSelector(action)];
}

#pragma mark - Initialization

- (void)awakeFromNib
Expand Down Expand Up @@ -221,6 +236,37 @@ - (void)setBounds:(CGRect)bounds
}
}

#pragma mark - Menu actions

- (BOOL)respondsToSelector:(SEL)aSelector
{
if ([jsqMessagesCollectionViewCellActions containsObject:NSStringFromSelector(aSelector)]) {
return YES;
} else {
return [super respondsToSelector:aSelector];
}
}

- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([jsqMessagesCollectionViewCellActions containsObject:NSStringFromSelector(anInvocation.selector)]) {
id sender;
[anInvocation getArgument:&sender atIndex:0];
[self.delegate messagesCollectionViewCell:self didPerformAction:anInvocation.selector withSender:sender];
} else {
[super forwardInvocation:anInvocation];
}
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
if ([jsqMessagesCollectionViewCellActions containsObject:NSStringFromSelector(aSelector)]) {
return [NSMethodSignature signatureWithObjCTypes: "v@:@"];
} else {
return [super methodSignatureForSelector:aSelector];
}
}

#pragma mark - Setters

- (void)setBackgroundColor:(UIColor *)backgroundColor
Expand Down

0 comments on commit c4d38a8

Please sign in to comment.