首页 > 解决方案 > 在快速删除 TableViewCell 之前弹出警报用户?

问题描述

标题基本上是这样说的;很惊讶我在堆栈概述中找不到任何东西,但没有人帮助我或处于目标 C 中

我有一个带有项目列表的表格视图和一个允许用户删除行的编辑按钮(也可以“滑动删除”)。基本上,我想要一个弹出警报,上面写着“你确定要删除(行名)”,其中行名是要删除的行的名称。从我发现/尝试的内容来看,我可以获得弹出窗口,但每次按下编辑按钮或向右滑动时它都会显示。我只希望在用户按下“删除”时出现弹出窗口。

并且,显然,从弹出窗口中,如果他们按取消它应该取消,如果他们按删除它应该删除

你一般是怎么做的?

对不起,我是个菜鸟

标签: iosswiftuitableview

解决方案


您所要做的就是在按下按钮时显示警报并设置每个操作。

用这个替换你的commit editingStyle委托方法,data用你的数据数组替换变量:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        presentDeletionFailsafe(indexPath: indexPath)
    }
}

func presentDeletionFailsafe(indexPath: IndexPath) {
    let alert = UIAlertController(title: nil, message: "Are you sure you'd like to delete this cell", preferredStyle: .alert)

    // yes action
    let yesAction = UIAlertAction(title: "Yes", style: .default) { _ in
        // replace data variable with your own data array
        self.data.remove(at: indexPath.row)
        self.tableView.deleteRows(at: [indexPath], with: .fade)
    }

    alert.addAction(yesAction)

    // cancel action
    alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    present(alert, animated: true, completion: nil)
}

编辑

例子:

private let reuseId = "cellReuseId"

class SlideToDeleteViewController : UIViewController {
    lazy var tableView = createTableView()

    func createTableView() -> UITableView {
        let tableView = UITableView()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: reuseId)
        tableView.dataSource = self
        tableView.delegate = self
        return tableView
    }

    var data = ["one", "two", "three", "four"]

    override func loadView() {
        self.view = tableView
    }

    override func viewDidLoad() {
        super.viewDidLoad()
    }
}

extension SlideToDeleteViewController: UITableViewDataSource, UITableViewDelegate {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: reuseId)
        cell?.textLabel?.text = data[indexPath.row]
        return cell!
    }

    func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
        return true
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
        if editingStyle == .delete {
            presentDeletionFailsafe(indexPath: indexPath)
        }
    }

    func presentDeletionFailsafe(indexPath: IndexPath) {
        let alert = UIAlertController(title: nil, message: "Are you sure you'd like to delete this cell", preferredStyle: .alert)

        // yes action
        let yesAction = UIAlertAction(title: "Yes", style: .default) { _ in
            // put code to remove tableView cell here
            self.data.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath], with: .fade)
        }

        alert.addAction(yesAction)

        // cancel action
        alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

        present(alert, animated: true, completion: nil)
    }
}

推荐阅读