ios - 在删除一行 UITableView 之前发出警告

标签 ios swift uitableview swift3

这是我尝试创建的代码,但它不起作用,它首先显示打印行,然后创建“警报”,如果我选择是,它会在我下次单击“删除”时删除该行.

// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // Delete the row from the data source
        crearAlertaDoble(titulo: "¿Seguro que deseas eliminar este calendario?", mensaje: "")
        print("opcion elegida: \(opcionAlertaMensaje)")
        if (opcionAlertaMensaje == 1) {
            objetoContenedor.calendarios.remove(at: indexPath.row) //WIP, MOSTRAR MENSAJE SI ESTA SEGURO
            tableView.deleteRows(at: [indexPath], with: .fade)
            opcionAlertaMensaje = 2
        } else {

        }

    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}

这是警报代码:

func crearAlertaDoble(titulo: String, mensaje: String) {
    let alert = UIAlertController(title: titulo, message: mensaje, preferredStyle: .alert)

    let botonUno = UIAlertAction(title: "NO!", style: UIAlertActionStyle.destructive, handler: { (action) -> Void in
        self.opcionAlertaMensaje = 0
    } )
    let botonDos = UIAlertAction(title: "Si", style: UIAlertActionStyle.default, handler: { (action) -> Void in
        self.opcionAlertaMensaje = 1
    } )

    alert.addAction(botonDos)
    alert.addAction(botonUno)

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

有什么建议吗?

最佳答案

显示警报 Controller 的方法是异步工作的。您不能在调用方法后立即同步处理结果。

有几种解决方案,其中之一是添加完成处理程序:

func crearAlertaDoble(titulo: String, mensaje: String, completion:@escaping (Int) ->()) {
    let alert = UIAlertController(title: titulo, message: mensaje, preferredStyle: .alert)

    let botonUno = UIAlertAction(title: "NO!", style: .destructive, handler: { action in
        completion(0)
    } )
    let botonDos = UIAlertAction(title: "Si", style: .default, handler: { action in
        completion(1)
    } )

    alert.addAction(botonDos)
    alert.addAction(botonUno)

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

并在 tableView:commit:forRowAt: 中使用它

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete {
        // Delete the row from the data source
        crearAlertaDoble(titulo: "¿Seguro que deseas eliminar este calendario?", mensaje: "") { result in
            print("opcion elegida: \(result)")
            if result == 1 {
                self.objetoContenedor.calendarios.remove(at: indexPath.row) //WIP, MOSTRAR MENSAJE SI ESTA SEGURO
                self.tableView.deleteRows(at: [indexPath], with: .fade)
            } else {

            }
        }

    } else if editingStyle == .insert {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }    
}

关于ios - 在删除一行 UITableView 之前发出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43833765/

相关文章:

iOS 自动布局 : Dynamic height for table view cell

ios - Swift 在另一个线程的 mapView 中显示注释

swift - 使用 Swift Package Manager 抑制来自依赖项的警告

xcode - UITableViewCell 与 UITextView 在滚动之前未对齐和剪切

ios - Facebook iOS SDK 3.1 在后续调用 FBSession openWithBehavior 时崩溃

ios - 为什么我无法在新的 iPad Xamarin 应用程序中构建?

ios - 导航栏标题 View 与左栏按钮项目重叠

ios - 更改两个文本字段的颜色

objective-c - 缓存 NSData(带有 UIImages)

swift - 如何在 SWIFT 的 UITableViewController 中切换单元格?