如何实现“滑动删除”列表中的项--swift

本文介绍了在Swift中如何实现在UITableView中实现左滑删除功能。通过重写`tableView(_:canEditRowAtIndexPath:)`和`tableView(_:commitEditingStyle:forRowAtIndexPath:)`方法,允许表格视图的编辑,并在用户滑动删除时从数据源移除相应项,更新表格视图状态。
摘要由CSDN通过智能技术生成

如何实现“滑动删除”呢?

同样是在tableView里面操作这些代码:


// Override to support conditional editing of the table view.左滑删除

    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {

        // Return false if you do not want the specified item to be editable.

        return true

    }

    

    // Override to support editing the table view.左滑删除

    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {

        print("delete table view cell")

        if editingStyle == .Delete {

          

            //获取数组中要删除的数据

            let this_password = self.todos[indexPath.row]

            this_password.delete { (result) in

                

                if(result.isSuccess){

                    // 根据 result.isSuccess 可以判断是否删除成功

                    self.todos.removeAtIndex(indexPath.row)

                    

                    //删除对应的cell

                    tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)

                    

                } else {

                    if (result.error != nil){

                        print(result.error?.reason)

                        // 如果删除失败,可以查看是否当前正确使用了 ACL

                    }

                }

            }

            

        }



你可以看到,我们成功的删除掉了第一条数据:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值