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

2022-04-01  本文已影响0人  CYC666

如图下所示
自定义一个视图,放在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)
            
        }
    }
上一篇下一篇

猜你喜欢

热点阅读