Add swipe to delete UITableViewCell

后端 未结 25 1632
暖寄归人
暖寄归人 2020-11-30 17:50

I am making a CheckList application with a UITableView. I was wondering how to add a swipe to delete a UITableViewCell.

This is my ViewCont

25条回答
  •  生来不讨喜
    2020-11-30 18:17

    here See my result Swift with fully customizable button supported

    Advance bonus for use this only one method implementation and you get a perfect button!!!

     func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
            let action = UIContextualAction(
                style: .destructive,
                title: "",
                handler: { (action, view, completion) in
    
                    let alert = UIAlertController(title: "", message: "Are you sure you want to delete this incident?", preferredStyle: .actionSheet)
    
                    alert.addAction(UIAlertAction(title: "Delete", style: .destructive , handler:{ (UIAlertAction)in
                        let model = self.incedentArry[indexPath.row] as! HFIncedentModel
                        print(model.incent_report_id)
                        self.incedentArry.remove(model)
                        tableView.deleteRows(at: [indexPath], with: .fade)
                        delete_incedentreport_data(param: ["incent_report_id": model.incent_report_id])
                        completion(true)
                    }))
    
                    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler:{ (UIAlertAction)in
                        tableView.reloadData()
                    }))
    
                    self.present(alert, animated: true, completion: {
    
                    })
    
    
            })
            action.image = HFAsset.ic_trash.image
            action.backgroundColor = UIColor.red
            let configuration = UISwipeActionsConfiguration(actions: [action])
            configuration.performsFirstActionWithFullSwipe = true
            return configuration
    }
    

提交回复
热议问题