2017-05-07 16 views
0

これは私が作成しようとしたコードですが、動作しません。最初に印刷行を表示してから、「警告」を作成します。次回は[削除]をクリックします。UITableViewの行を削除する前に警告する

// 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) 
} 

答えて

0

これは、UIAlertActionがクロージャハンドラで削除を行うためです。それは非同期です。あなたのコードでは、まずcommitEditing()メソッドのすべてのコードを実行します。また、ユーザーがSiをタップした後、またはノーハンドラーが起動しなくても。しかし、それは何もしません。なぜなら、ハンドラでの削除については何もないからです。

あなたはこのようにそれを修正することができます:あなたが選択したオプションを保存する必要はありませんが、あなたはそれを格納する必要がある場合は、その場合には

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if editingStyle == .delete { 

     let alert = UIAlertController(title: "¿Seguro que deseas eliminar este calendario?", message: "", 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.objetoContenedor.calendarios.remove(at: indexPath.row) 
      tableView.deleteRows(at: [indexPath], with: .fade) 

     }) 

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

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

    } 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 
    } 
} 

- あなたは(もハンドラで)それを追加することができます

+0

でそれを使用するには、それが動作するので、ありがとうございました! :D –

2

アラートコントローラを表示する方法は、非同期で動作します。メソッドを呼び出した直後には、結果を同期して処理することはできません。

そのうちの一つが完了ハンドラを追加することで、いくつかの解決策、あります

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 
    }  
} 
+0

ありがとう、これは他のコメントとこれは働く:D –

関連する問題