随笔 - 400,  文章 - 0,  评论 - 7,  阅读 - 21万

 

 

复制代码
    /**侧滑删除实现的三个方法:
     *  canEditRowAt :设为true 才能侧滑或其他操作
     *  editActionsForRowAt (11之前侧滑删除)
     *  trailingSwipeActionsConfigurationForRowAt (11之后的侧滑删除)
     */
    //canEditRowAt :设为true 才能侧滑或其他操作
    @objc optional func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool
    
    /// 11之前侧滑删除
    @objc optional func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]?
    
    ///11之后的侧滑删除
    @available(iOS 11.0, *)
    @objc optional func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
复制代码

 

 

复制代码
    func tableViewl(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
//显示/隐藏
        let showStateAction = UITableViewRowAction.init(style: .default, title: showStateTitle) { [weak self] (res, index) in
            }
        }
        showStateAction.backgroundColor = UIColor.red

        //1删除
        let deleteAction = UITableViewRowAction(style: UITableViewRowAction.Style.default, title: "删除") { [weak self] (res, index) in
        }
        deleteAction.backgroundColor = UIColor.green
        return [showStateAction, deleteAction]
    }
复制代码

 

 

在iOS11之后多了一个方法,并且 侧滑会直接 左滑到底, 造成误删除, 需要禁用

UISwipeActionsConfiguration的performsFirstActionWithFullSwipe 属性,改为false
复制代码
    //尾部滑动事件按钮(左滑按钮)
        @available(iOS 11.0, *)
        func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {

            let showStateAction = UIContextualAction(style: UIContextualAction.Style.destructive, title: "文字") { [weak self](action:UIContextualAction, view:UIView, handle:@escaping (Bool) -> Void) in

            }
            showStateAction.backgroundColor = UIColor.init(hexString: "#D1D1D1")

            //删除
            let  deleteAction = UIContextualAction(style: UIContextualAction.Style.destructive, title: "删除") { [weak self](acction:UIContextualAction, view:UIView, handle:@escaping (Bool) -> Void) in

            }
            deleteAction.backgroundColor = UIColor.init(hexString: "#F53943")

            //destructive图片
            let  destructiveImageAction = UIContextualAction(style: UIContextualAction.Style.destructive, title: nil) { [weak self](acction:UIContextualAction, view:UIView, handle:@escaping (Bool) -> Void) in
                self?.deleteOrder(by: indexPath.section)
            }
            if let ima = UIImage.init(named: "tempOrderDelete"){
                ///设置图片
                destructiveImageAction.image = ima
                ///和cell背景色一样就可以
                destructiveImageAction.backgroundColor  = UIColor.init(red: 195, green: 76, blue: 93, alpha: 1)
            }

            let configuration =  UISwipeActionsConfiguration(actions: [showStateAction, deleteAction, destructiveImageAction])
            configuration.performsFirstActionWithFullSwipe = false
            return configuration
        }
复制代码

 


 

参考:

Swift - 自定义tableViewCell滑动事件按钮2(使用iOS11的滑动按钮接口)

https://www.hangge.com/blog/cache/detail_1891.html

 

iOS tableView左滑删除的两种方法

https://blog.csdn.net/qq_22955427/article/details/79347404?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-29&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-29

posted on   懂事长qingzZ  阅读(1512)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示