swift tableview中添加侧滑删除功能 类似qq删除

本文介绍如何在Swift中为UITableView添加类似于QQ的侧滑删除功能。通过实现UITableViewDelegate的三个方法:tableView(_:canEditRowAtIndexPath:), tableView(_:editingStyleForRowAtIndexPath:) 和 tableView(_:commitEditingStyle:forRowAtIndexPath:),可以实现单元格的编辑和删除操作。" 81272520,7523836,jQuery网页特效实践:第九章习题解析,"['jQuery', '前端开发', '特效实现', '网页交互']

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

UITableViewDelegate 中继承三个方法

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

func tableView(tableView:UITableView, editingStyleForRowAtIndexPath indexPath:NSIndexPath) -> UITableViewCellEditingStyle

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

然后分别在三分方法中添加业务逻辑。

<pre name="code" class="objc"><pre name="code" class="plain">//mark 设置可以进行编辑
    func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool{
        println("mark 设置可以进行编辑")
        return true
    }
    // mark 设置编辑的样式
    func tableView(tableView: UITableView, editingStyleForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCellEditingStyle{
        println("mark 设置编辑的样式")
        return UITableViewCellEditingStyle.Delete
    }
    //删除cell
    func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath){
        println("删除cell")
        if editingStyle == UITableViewCellEditingStyle.Delete{
            //1,从本地数据库将数据移除
            //调用dbhelper中的delete方法删除cocodata中的数据
            dbhelper.deleteConversationBySay(conlist[indexPath.row].say)
            conlist.removeAtIndex(indexPath.row)
           
            //2.reload
            //直接使用reload方法界面的变化非常迅速,用户体验非常不好
            //tv.reloadData()
            //这个具有动画效果
            self.tv.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
        }
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值