美文网首页
iOS swift 自定义系统自带的UITableView左滑删

iOS swift 自定义系统自带的UITableView左滑删

作者: CYC666 | 来源:发表于2022-04-01 14:54 被阅读0次

    如图下所示
    自定义一个视图,放在cell的右边边缘外,覆盖左滑删除的删除按钮


    image.png

    其它就是补充一些系统UITableView左滑删除的代码

    // MARK: 左滑删除
        func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
            return true
        }
        
        // 不设置这个,可以避免左滑偶尔出现红底的bug
        // func tableView(_ tableView: UITableView, titleForDeleteConfirmationButtonForRowAt indexPath: IndexPath) -> String? {
        //    return "删除"
        // }
    
        func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
            
            if indexPath.row < self.dataList.count {
                
                let model = self.dataList[indexPath.row]
                
                let alert = UIAlertController(title: "确定要删除?", message: "", preferredStyle: .alert)
                
                let saveAction = UIAlertAction(title: "删除", style: .default) { (action :UIAlertAction!) in
                    
                }
                
                let cancelAction = UIAlertAction(title: "取消", style: .cancel) { (action: UIAlertAction) in
                    
                }
                
                saveAction.setValue(UIColor.red, forKey: "_titleTextColor")
                alert.addAction(saveAction)
                alert.addAction(cancelAction)
                
                present(alert, animated: true, completion: nil)
                
            }
        }
    

    相关文章

      网友评论

          本文标题:iOS swift 自定义系统自带的UITableView左滑删

          本文链接:https://www.haomeiwen.com/subject/zreajrtx.html