UA-17470720-3

Jump to content


Photo
- - - - -

UITableViewDelegate methods shouldShowMenuForRowAtIndexPath and canPerformAction:forRowAtIndexPath:withSender:


  • Please log in to reply
8 replies to this topic

#1 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 18 November 2015 - 09:06 AM

Hi Tarek,

 

Can I request that STV implements these two UITableViewDelegate methods:


- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}


- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)anAction forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return NO;
}

 

 

Then a SCTableViewCell subclass can use -(BOOL)canPerformAction:(SEL)action withSender:(id)sender to provide its own actions to respond to.

 

 


Edited by Dave Guerin, 18 November 2015 - 09:08 AM.

Cheers,

Dave

www.dgapps.ie

#2 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 18 November 2015 - 10:10 AM

Will do in our upcoming release, thanks Dave.



#3 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 18 November 2015 - 10:31 AM

Hi Tarek,

 

Great, thanks very much.

 

Looking forward 5.2 :-)


Cheers,

Dave

www.dgapps.ie

#4 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 04 December 2015 - 10:46 AM

Hi Tarek,

 

I've seen the new cellActions shouldShowMenu, canPerformAction, performAction that were added in STV 5.2.0, thank you very much for them.

 

I'm having trouble getting them to work though. Here's my code:

 

 

 customCell.cellActions.shouldShowMenu = ^BOOL(SCTableViewCell *cell, NSIndexPath *shouldShowMenuIndexPath) {
                NSLog(@"customCell cellActions shouldShowMenu YES");
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
                /*
                 Avoids "undeclared selector" warnings for the @selector()...
                 */
                UIMenuItem *duplicateSong = [[UIMenuItem alloc] initWithTitle:@"Duplicate Song" action:@selector(duplicateSong:)];
#pragma clang diagnostic pop

                [[UIMenuController sharedMenuController] setMenuItems:@[duplicateSong]];
                [[UIMenuController sharedMenuController] update];

                return YES;
            };

            customCell.cellActions.canPerformAction = ^BOOL(SCTableViewCell *cell, NSIndexPath *canPerformActionIndexPath, SEL action, id sender) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
                if (action == @selector(duplicateSong:)) {
                    NSLog(@"customCell cellActions canPerformAction:%@ YES",NSStringFromSelector(action));
                    return YES;
                }
#pragma clang diagnostic pop

                NSLog(@"customCell cellActions canPerformAction:%@ NO",NSStringFromSelector(action));
                return NO;
            };

            customCell.cellActions.performAction = ^(SCTableViewCell *cell, NSIndexPath *performActionIndexPath, SEL action, id sender) {

                NSLog(@"PERFORM %@",NSStringFromSelector(action));
            };

 

 

And here's the NSLog output:

 

 

customCell cellActions shouldShowMenu YES
customCell cellActions canPerformAction:cut: NO
customCell cellActions canPerformAction:copy: NO
customCell cellActions canPerformAction:select: NO
customCell cellActions canPerformAction:selectAll: NO
customCell cellActions canPerformAction:paste: NO
customCell cellActions canPerformAction:delete: NO
customCell cellActions canPerformAction:_promptForReplace: NO
customCell cellActions canPerformAction:_transliterateChinese: NO
customCell cellActions canPerformAction:_showTextStyleOptions: NO
customCell cellActions canPerformAction:_define: NO
customCell cellActions canPerformAction:_addShortcut: NO
customCell cellActions canPerformAction:_accessibilitySpeak: NO
customCell cellActions canPerformAction:_accessibilitySpeakLanguageSelection: NO
customCell cellActions canPerformAction:_accessibilityPauseSpeaking: NO
customCell cellActions canPerformAction:_share: NO
customCell cellActions canPerformAction:makeTextWritingDirectionRightToLeft: NO
customCell cellActions canPerformAction:makeTextWritingDirectionLeftToRight: NO
customCell cellActions canPerformAction:duplicateSong: YES

 

So I should get a "Duplicate Song" menu item appear on a long tap on the customCell, but I get nothing.

 

Returning YES for all actions in canPerformAction and the "Cut" "Copy" "Select All" and "Paste" menu items appear.

 

 

Am I missing something, am I adding my custom UIMenuItem incorrectly?

 

 

 

 

The documentation for canPeformAction said it was

 

return (action == (copy:));

 

 

to only allow just a Copy menu item. I think it needs to be 

 

return (action == @selector(copy:));

to avoid a complier error.


Cheers,

Dave

www.dgapps.ie

#5 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 05 December 2015 - 09:12 AM

Hi Dave,

 

I think you're confusing two issues here:

 

a. cellActions.canPerformAction is simply a proxy for the UITableViewDelegate method with the same name, which doesn't support custom actions, as per Apple's documentation. To add custom actions, you should create a custom cell and define this method there (ultimately provided by UIResponder). For instance:

 

@interface MyCustomCell : SCCustomCell
@end

@implementation MyCustomCell

-(BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
    // only show 'Copy' and 'Duplicate Song'
    return (action == @selector(copy:) || action == @selector(duplicateSong:));
}

- (void)duplicateSong:(id)sender
{
    NSLog(@"In duplicateSong!");
}

@end

 

 

b. cellActions.shouldShowMenu gets called many times as you long tap cells, so you should add the menu items in viewDidLoad or viewDidAppear for instance:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    UIMenuItem *duplicateSong = [[UIMenuItem alloc] initWithTitle:@"Duplicate Song" action:@selector(duplicateSong:)];
    [[UIMenuController sharedMenuController] setMenuItems:@[duplicateSong]];
    [[UIMenuController sharedMenuController] update];
}

 

 

Hope this helps.



#6 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 05 December 2015 - 09:16 AM

The documentation for canPeformAction said it was 'return (action == (copy:);'

 

I can't find this at all. All I see in our docs is the correct 'return (action == @selector(copy:);'. Would you please point me to the exact line number in case I am missing something? Thanks!



#7 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 07 December 2015 - 04:58 AM

Hi Tarek,

 

I knew I was confused :-)

 

I already had a duplicateSong menu item working by adding the UITableViewDelegate methods tableView:shouldShowMenuForRowAtIndexPath: and tableView:canPerformAction:forRowAtIndexPath:withSender: to the STV code and building a custom version of the framework. 

 

So now I can use cellActions.shouldShowMenu to get the menu to appear, and then use my SCCustomCell subclass to add the duplicateSong menu item and respond to canPerformAction:withSender: and then also perform the action. Hurrah! All working, and no adding code to STV itself :-)


Cheers,

Dave

www.dgapps.ie

#8 Dave Guerin

Dave Guerin

    Forum Master

  • STV 5.0 Pro
  • PipPipPipPipPipPipPip
  • 539 posts
  • LocationIreland
Reputation: 134
Popular

Posted 07 December 2015 - 05:10 AM

I can't find this at all. All I see in our docs is the correct 'return (action == @selector(copy:);'. Would you please point me to the exact line number in case I am missing something? Thanks!

 

 

Mmm, it's line 685 in SCCellActions.h, but that has the @selector.

 

However, in Xcode, when I move the cursor into canPerformAction in my code then Xcode quick help strips out the @selector, that's where I cut and pasted the block code from, as Xcode doesn't autocomplete all of that unfortunately.

 

I'm not sure you can do anything about that, I've noticed that the @ from a string is also stripped, for example the NSLog in cellActions.didSelect is shown in Xcode quick help as

 

NSLog("Cell at indexPath:%@ has been selected.", indexPath); 

where as the documentation in the header file is:

 

NSLog(@"Cell at indexPath:%@ is about to be deselected.", indexPath);
 

Cheers,

Dave

www.dgapps.ie

#9 Tarek

Tarek

    Forum Admin

  • Administrators
  • 3670 posts
Reputation: 452
Popular

Posted 08 December 2015 - 04:51 AM

Glad everything is working well now :)

 

Re docs: Yes very strange, seems to be a bug in Xcode that removes @selector from the integrated editor help popups. 


  • Dave Guerin likes this




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users