- - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- return YES;
- }
- - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender
- {
- return YES;
- }
- - (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:@""];
- [tableView 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];
- [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
- }
- }
如果希望只出现一个或两个特定的菜单选项,可以在- (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender 中加入相应判断,return YES则出现,return NO则不出现了。
第三个函数就是我们处理逻辑的地方,我这里只是测试简单纯文本Cell。
这样长按就可以出现三个功能菜单选项,点击进行相应操作。
【编辑推荐】