Swift 自定义TableView滑动删除按钮

viewDidLayoutSubviews 调用方法修改滑动按钮

func customDeleteBgView (){
        if #available(iOS 11.0, *) {
            for subview in self.tableView.subviews where subview.isKind(of: NSClassFromString("UISwipeActionPullView")!) {
                subview.backgroundColor = .white
                
                for view in subview.subviews where view.isKind(of: NSClassFromString("UISwipeActionStandardButton")!) {

                    view.backgroundColor = .white
                    let bgView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: FITSCALE(num: 118) + 24.0))
                    bgView.backgroundColor = .RCColor
                    bgView.cornerRadius = 8
                    view.insertSubview(bgView, at: 0)
                    if let indexpath = self.editeIndexPath ,indexpath.row == 0 {
                        bgView.benY = 10
                        
                    } else {
                        bgView.benY = 0
                    }
                }
            }
            
        } else {    // IOS 10以及以下
            for subview in self.tableView.subviews where subview.isKind(of: NSClassFromString("UITableViewCellDeleteConfirmationView")!) {
                subview.backgroundColor = .white
                
                for view in subview.subviews where view.isKind(of: UIButton.self) {
                    view.backgroundColor = .white
                    let bgView = UIView.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: FITSCALE(num: 118) + 24.0))
                    bgView.backgroundColor = .RCColor
                    bgView.cornerRadius = 8
                    view.insertSubview(bgView, at: 0)
                    if let indexpath = self.editeIndexPath ,indexpath.row == 0 {
                        bgView.benY = 10
                        
                    } else {
                        bgView.benY = 0
                    }
                }
            }
        }
    }

实现TableViewDelegate

func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool{
        return true
    }
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String?{
        return "删除"
    }
    
    func tableView(_ tableView: UITableView, willBeginEditingRowAt indexPath: IndexPath) {
        self.view.setNeedsLayout()
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
       
    }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容