2017-05-07 1 views
0

이것은 생성하려고 시도했지만 작동하지 않는 코드입니다. 인쇄 선을 처음 표시 한 다음 '경고'를 만듭니다. 예를 선택하면 iit이 행을 삭제합니다. 다음 번에 삭제를 클릭합니다.UITableView 행을 삭제하기 전에 경고

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

어떤 제안 :

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

그리고 이것은 경고 코드는?

답변

0

UIAlertAction이 클로저 처리기에서 제거하기 때문입니다. 비동기식입니다. 귀하의 코드에서 먼저 commitEditing() 메소드의 모든 코드를 실행하고 제거하십시오. 그리고 사용자가 Si를 누르거나 No-handler가 발동 한 후에 만. 그러나 핸들러에서 삭제하는 것에 대해서는 아무 것도 없기 때문에 아무 것도하지 않습니다.

당신은 (또한 핸들러에) 추가 할 수 있습니다 - 당신이 선택한 옵션을 저장할 필요하지 않는 경우

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

tableView:commit:forRowAt: 너무 감사에서 그것을

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

를 사용, 그것은 작동합니다! : D –

2

경고 컨트롤러를 표시하는 방법은 비동기 적으로 작동합니다. 메서드를 호출 한 직후에 결과를 동 기적으로 처리 할 수 ​​없습니다.

여러 솔루션이 있습니다, 그 중 하나가 완료 핸들러를 추가하는 것입니다

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 –

관련 문제