Saturday, June 8, 2013

UITableView tableView canPerformAction forRowAtIndexPath withSender example in Objective C (iOS).


UITableView tableView canPerformAction forRowAtIndexPath withSender

Asks the delegate if the editing menu should omit the Copy or Paste command for a given row.

- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender

Parameters of [UITableView tableView canPerformAction forRowAtIndexPath withSender]
tableView
The table-view object that is making this request.
action
A selector type identifying the copy: or paste: method of the UIResponderStandardEditActions informal protocol.
indexPath
An index-path object locating the row in its section.
sender
The object that initially sent the copy: or paste: message. T

Return Value of [UITableView tableView canPerformAction forRowAtIndexPath withSender]
YES if the command corresponding to action should appear in the editing menu, otherwise NO. The default value is NO.

Discussion of [UITableView tableView canPerformAction forRowAtIndexPath withSender]
This method is invoked after tableView:shouldShowMenuForRowAtIndexPath:. It gives the developer the opportunity to exclude one of the commands—Copy or Paste—from the editing menu. For example, the user might have copied some cell content from one row but wants to paste into another row that doesn’t take the copied content. In a case like this, return NO from this method.

UITableView tableView canPerformAction forRowAtIndexPath withSender example.
-(void)copy:(id)sender {
    [[UIPasteboard generalPasteboard] setString:detailTextLabel.text];
}

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if(action == @selector(copy:)) {
        return YES;
    }
    else {
        return [super canPerformAction:action withSender:sender];
    }
}

Example of [UITableView tableView canPerformAction forRowAtIndexPath withSender].
@interface MenuTableViewCell : UITableViewCell {
}
- (IBAction)copy:(id)sender;
- (void)showMenu;

@end

@implementation MenuTableViewCell

- (BOOL)canBecomeFirstResponder {
    return YES;
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(copy:)) {
        return YES;
    }
    return NO;
}
- (IBAction)copy:(id)sender {
}
- (void)showMenu {
    [[UIMenuController sharedMenuController] setMenuVisible:NO animated:YES];
    [self becomeFirstResponder];
    [[UIMenuController sharedMenuController] update];
    [[UIMenuController sharedMenuController] setTargetRect:CGRectZero inView:self];
    [[UIMenuController sharedMenuController] setMenuVisible:YES animated:YES];

}

@end

UITableView tableView canPerformAction forRowAtIndexPath withSender example.
- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)actionforRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
{
    NSLog(@"canPerformAction Method entered");
    [self switchTitle:tableView indexPath:indexPath];
    return YES;
}

End of UITableView tableView canPerformAction forRowAtIndexPath withSender example article.