iOS tableView左滑删除按钮的自定义 iOS tableView左滑删除按钮的自定义

iOS tableView左滑删除按钮的自定义

本文仅做日常开发记录
遇到一个需求如下图:


左滑删除的时候,要删除按钮也有圆角
实现方式就是拿到删除的那个视图,然后修改layer,代码如下:
如有小伙伴也需要,仅供参考,因为这样写,是不安全的,直接上代码:


extension ViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! TestCell
        cell.backgroundColor = UIColor.white
        if indexPath.row % 2 == 0 {
            cell.contentView.backgroundColor = .systemYellow
        }
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        90
    }
    
    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        guard editingStyle == .delete else {
            return
        }
        //点击删除会触发
         print("____shan chu cell")
    }
    
    
    func tableView(_ tableView: UITableView, editingStyleForRowAt indexPath: IndexPath) -> UITableViewCell.EditingStyle {
        return .delete
    }
    
    func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        return "删除"
    }
    
    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }
    
    func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //guard let cell = self.feelCell else { return  }
        //cell.topLayer?.frame = cell.bgImageView.bounds
        cell.contentView.layer.cornerRadius = 15
        cell.contentView.layer.masksToBounds = true
        let img = UIImageView()
        img.contentMode = .scaleAspectFill
    }
    
    
    
    @available(iOS 11.0, *)
    func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
        let delete = UIContextualAction(style: UIContextualAction.Style.normal, title: "删除") { [weak self] _, _, complete in
            print("___删除了 cell")
            complete(true)
        }
        //delete.image = UIImage(named: "aaa")
        //delete.imageView!.contentMode = .scaleAspectFill
        //delete.image.contentMode = .scaleAspectFill

        delete.backgroundColor = UIColor.systemGreen
        let action = UISwipeActionsConfiguration(actions: [delete])
        action.performsFirstActionWithFullSwipe = false
        return action
    }
    
    //滑动删除的时候会调用
    func tableView(_ tab: UITableView, willBeginEditingRowAt: IndexPath) {
        print("____开始删除")
        test()
    }
    

为删除按钮添加圆角


extension ViewController {
    //iOS 11 和iOS 12获取删除按钮的方法
    private func test() {
        for subView in tableView.subviews {

            print("___sub:___\(subView)____\(subView.classForCoder)")
            
            //_UITableViewCellSwipeContainerView
            if NSStringFromClass(subView.classForCoder) == "_UITableViewCellSwipeContainerView"{
                for sub in subView.subviews {
                    if NSStringFromClass(sub.classForCoder) == "UISwipeActionPullView" {
                        sub.backgroundColor = .clear
                        if let delBtn: UIButton = sub.subviews.first as? UIButton  {
                            //此处可以拿到删除按钮后做一些相应的操作
                            print(">>>完全自定义___\(delBtn)")
                            delBtn.isHidden = false
                            delBtn.layer.cornerRadius = 9.0
                            delBtn.clipsToBounds = true
                        }
                        sub.backgroundColor = .systemRed
                        sub.layer.cornerRadius = 9.0
                        sub.layer.masksToBounds = true
                    }
                }
            }
        }
    }
    
}


实现效果如下:


有需要的小伙伴,可以自取,亲测没问题

ps:

iOS9、iOS11、iOS13具体获取到对应删除按钮的方式不同
iOS9、iOS11添加按钮的方式有所不同
自定义的的控件的宽度存在一些差异
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章