4.8 Enabling Swipe Deletion of Table View Cells

本文介绍在iOS应用中如何使用UITableView实现两种不同的行删除操作:一种是通过设置编辑模式,另一种是通过滑动手势触发删除选项。

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

你是否曾经在tableView列表中删除某些行,你是怎么删除的呢?
通过点击某个按钮,然后列表每一行的左边出来一个红色减号,点击然后右边出来一个删除按钮,然后删除?(这种情况时设置列表的editing属性来实现的)
当然了,还有一种情况是在这一行上划一下,然后就出来一个删除按钮,这功能是怎么实现的呢?

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];//苹果说这个一定要写,删了不知会咋样,小例子没出现问题
    [_tableView setEditing:editing animated:animated];
   
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [tableRows removeObjectAtIndex:indexPath.row]; //先删除数据源里的数据,再从界面上删除
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationLeft];
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值