Saturday, June 8, 2013

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


UITableView tableView performAction forRowAtIndexPath withSender

Tells the delegate to perform a copy or paste operation on the content of a given row.

- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender

Parameters of [UITableView tableView performAction 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.

Discussion of [UITableView tableView performAction forRowAtIndexPath withSender]
The table view invokes this method for a given action if the user taps Copy or Paste in the editing menu. The delegate can do whatever is appropriate for the action; for example, for a copy, it can extract the relevant cell content for the row at indexPath and write it to the general pasteboard or an application (private) pasteboard. See UIPasteboard Class Reference for further information.

UITableView tableView performAction forRowAtIndexPath withSender example.
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

-(BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return (action == @selector(copy:));
}

- (BOOL)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
    return YES;
}

Example of [UITableView tableView performAction forRowAtIndexPath withSender].
- (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
if (action == @selector(copy:)) {
[UIPasteboard generalPasteboard].string = [dataArray objectAtIndex:indexPath.row];
}
if (action == @selector(cut:)) {
[UIPasteboard generalPasteboard].string = [dataArray objectAtIndex:indexPath.row];
[dataArray replaceObjectAtIndex:indexPath.row withObject:@""];
[tbl reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
if (action == @selector(paste:)) {
NSString *pasteString = [UIPasteboard generalPasteboard].string;
NSString *tmpString = [NSString stringWithFormat:@"%@ %@", [dataArray objectAtIndex:indexPath.row], pasteString];
[dataArray replaceObjectAtIndex:indexPath.row withObject:tmpString];
[tbl reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

End of UITableView tableView performAction forRowAtIndexPath withSender example article.