首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >删除单元格Swift Xcode前的警报控制器

删除单元格Swift Xcode前的警报控制器
EN

Stack Overflow用户
提问于 2018-08-24 04:22:35
回答 2查看 1.5K关注 0票数 0

我想知道如何在删除单元格之前显示删除确认(警告)。单元格是带有信息的字符,所以如果用户错误地删除(刷)了一个单元格,这将是不好的。

这是允许我删除行的代码片段。我正在使用Xcode 10 / Swift

代码语言:javascript
运行
复制
// Delete Rows
override func tableView(_ tableView: UITableView,
               trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let modifyAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("Update action ...")
        success(true)

        self.namesArray.remove(at: indexPath.row)
        self.imagesArray.remove(at: indexPath.row)

        self.tableView.deleteRows(at: [indexPath], with: .fade)

    })
    modifyAction.image = UIImage(named: "delete")
    modifyAction.backgroundColor = .purple

    return UISwipeActionsConfiguration(actions: [modifyAction])

}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-24 04:42:24

您可以将显示UIAlertController的代码放入一个单独的方法中,当用户按下delete时可以调用该方法。下面的代码确保闭包中包含的所有表示逻辑都被分派到主线程。

代码语言:javascript
运行
复制
func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
    let modifyAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        DispatchQueue.main.async {
            self.showDeleteWarning(for: indexPath)
        }

        success(true)
    })

    modifyAction.image = UIImage(named: "delete")
    modifyAction.backgroundColor = .purple

    return UISwipeActionsConfiguration(actions: [modifyAction])
}

func showDeleteWarning(for indexPath: IndexPath) {
    //Create the alert controller and actions
    let alert = UIAlertController(title: "Warning Title", message: "Warning Message", preferredStyle: .alert)

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

    let deleteAction = UIAlertAction(title: "Delete", style: .destructive) { _ in
        DispatchQueue.main.async {
            self.namesArray.remove(at: indexPath.row)
            self.imagesArray.remove(at: indexPath.row)
            self.tableView.deleteRows(at: [indexPath], with: .fade)
        }
    }

    //Add the actions to the alert controller
    alert.addAction(cancelAction)
    alert.addAction(deleteAction)

    //Present the alert controller
    present(alert, animated: true, completion: nil)
}
票数 2
EN

Stack Overflow用户

发布于 2018-08-24 04:30:38

你可以试试

代码语言:javascript
运行
复制
override func tableView(_ tableView: UITableView,
               trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
{
    let modifyAction = UIContextualAction(style: .normal, title:  "Delete", handler: { (ac:UIContextualAction, view:UIView, success:(Bool) -> Void) in
        print("Update action ...")
        success(true)

         let alertView = UIAlertController(title: "", message: "Are you sure you want to delete the item ? ", preferredStyle: .alert)
         let okAction = UIAlertAction(title: "OK", style: .default, handler: { (alert) in
             self.namesArray.remove(at: indexPath.row)
             self.imagesArray.remove(at: indexPath.row)     
             self.tableView.deleteRows(at: [indexPath], with: .fade)
        })
        let cancelAction = UIAlertAction(title: "Cancel", style:.cancel, handler: { (alert) in
             print("Cancel")
        })
        alertView.addAction(okAction)
        alertView.addAction(cancelAction)
        self.present(alertView, animated: true, completion: nil)

    })
    modifyAction.image = UIImage(named: "delete")
    modifyAction.backgroundColor = .purple 
    return UISwipeActionsConfiguration(actions: [modifyAction])

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51993719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档