tableViewcell上点击删除按钮,对此cell进行删除,弹出UIAlertController提示框确认

需求

tableview上对列表中数据进行删除时弹出窗口进行再次确认
效果如图:
在这里插入图片描述

  • ios 8.0以上的参考,我用的UIAlertController,笔者刚开始学所以UIAlertView没用过

采坑

tableviewrow点击按钮对应的delegate editActionsForRowAtIndexPath实现时, 对应的点击事件UITableViewRowAction *deleteAction的block里面不应该进行删除操作(这样会出现,列表数据删除完毕后才弹框的效果).

解决方法

UIAlertAction *okAction即在UIAlertController的对应按钮事件block中进行数据删除和列表更新操作

代码

//这里是tableview的代理方法
-(NSArray<UITableViewRowAction *> *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    //deleteAction
    UITableViewRowAction *deleteAction = [UITableViewRowAction rowActionWithStyle:UITableViewRowActionStyleDestructive title:@"del" handler:^(UITableViewRowAction * _Nonnull action, NSIndexPath * _Nonnull indexPath)
    {
        NSLog(@"delete no.%ld item",indexPath.row);
        //这里我把row的行和 indexPath作为参数传进去了 为了删除数据和刷新tableview
        [self askalert: indexPath.row tableindex: indexPath];  //在弹出的alertController里面的block里对tableview就行操作
    }];
 
    return @[deleteAction];
}

//这里是UIAlertController对象创建的方法进行了封装,为了代码看起来简洁
- (void) askalert:(NSInteger)delItemindex tableindex:(nonnull NSIndexPath *)indexPath
{
    //因为用的arc ,为了防止内存泄漏用 __weak, __block是为了在block代码里使用这个tmp变量
    __weak __block XYZToDoListTableViewController *tmp = self;
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Tip" message:@"Are you sure?" preferredStyle:UIAlertControllerStyleAlert];
    
    //取消按钮事件,取消就啥都不做
    __block UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (cancelAction) {
            
        }
    }];
    
    //确认删除的按钮,这里进行数据删除和tableview刷新
    __block UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if (okAction) {
		
			//列表数组中删除数据
            [tmp.toDoItems removeObjectAtIndex:delItemindex];   
			
			//刷新tableview
            [tmp.tableView beginUpdates];
            [tmp.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
            [tmp.tableView endUpdates];
            [tmp savedata:tmp.toDoItems];  //这里是我的业务你们不用看 :> 就是保存数据而已
        }
    }];
    // 添加按钮事件
    [alert addAction:cancelAction];
    [alert addAction:okAction];
    // 显示操作
    [tmp presentViewController:alert animated:YES completion:nil];  
}

之前查了好久没查到,希望能帮到你

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

路途遥远gg

帮到你了就好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值