[课堂实践与项目]UITableView 长按Cell出现选择(剪切,复制,粘贴,全选)菜单并执行相应的操作

本文介绍了如何在UITableView中实现长按Cell显示剪切、复制、粘贴和全选等操作的菜单,并详细讲解了其实现过程,主要通过代理方法完成。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.我们首先来看看效果



2.实现的方法很简单,只需要实现代理中的三个方法即可。

//允许 Menu菜单
- (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//每个cell都会点击出现Menu菜单
- (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 = [self.array objectAtIndex:indexPath.row];
    }
    if (action == @selector(cut:)) {
        [UIPasteboard generalPasteboard].string = [self.array objectAtIndex:indexPath.row];
        [self.array replaceObjectAtIndex:indexPath.row withObject:@""];
        
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
        
        
    }
    
    if (action == @selector(paste:)) {
        NSString *pasteString = [UIPasteboard generalPasteboard].string;
        
        NSString *tempString = [NSString stringWithFormat:@"%@%@",[self.array objectAtIndex:indexPath.row],pasteString];
        
        [self.array replaceObjectAtIndex:indexPath.row withObject:tempString];
         [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }

}






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值