ios - 在快速删除 TableViewCell 之前弹出警报用户?
问题描述
标题基本上是这样说的;很惊讶我在堆栈概述中找不到任何东西,但没有人帮助我或处于目标 C 中
我有一个带有项目列表的表格视图和一个允许用户删除行的编辑按钮(也可以“滑动删除”)。基本上,我想要一个弹出警报,上面写着“你确定要删除(行名)”,其中行名是要删除的行的名称。从我发现/尝试的内容来看,我可以获得弹出窗口,但每次按下编辑按钮或向右滑动时它都会显示。我只希望在用户按下“删除”时出现弹出窗口。
并且,显然,从弹出窗口中,如果他们按取消它应该取消,如果他们按删除它应该删除
你一般是怎么做的?
对不起,我是个菜鸟
解决方案
您所要做的就是在按下按钮时显示警报并设置每个操作。
用这个替换你的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)
}
}
推荐阅读
- javascript - 将 dropzone.js 与 pica(或其他图像压缩/调整大小库)一起使用
- python - Python/Keras - 对模型使用拟合函数:没有为任何变量提供梯度
- r - 避免 geom_errorbar 中的“错误:美学必须是长度 1 或与数据 (9):y 相同”
- java - 使用 MongoRepository 保存/插入时出现 StackOverflowError
- java - Firebase 数据库运行循环中未捕获的异常仅在生产中崩溃
- javascript - 如何在集合中获取数据 Firebase 集合
- reactjs - 在 React 中自动生成 ID
- pandas - 熊猫数据框每行强制执行
- firebase - 从云存储中获取一些元数据到云功能
- python - 将我的 selenium 可执行文件移动到另一台计算机时,chrome 驱动程序不在路径中