1

estoy haciendo una app que guarda información en CoreData y la muestra al usuario en una TableView . Quisiera borrar las filas y los datos , creo que se puede implementar en esta seccion ?

// 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
           tableView.deleteRows(at: [indexPath], with: .fade)
       } 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
       }    
   }
   */

   /* 

Implemente este codigos se borra las filas pero los datos regresan despues de refrescar la app

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {


    if editingStyle == .delete {

     objetos.remove(at: indexPath.item)
        tableView.deleteRows(at: [indexPath], with: .automatic)

1 respuesta 1

0

puedes usar este codigo dentro del editingStyle:

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {

        switch editingStyle {
        case .delete:
            // remueve el item eliminado del modelo
            let appDel: AppDelegate = UIApplication.shared.delegate as! AppDelegate
            let managedObjectContext = appDel.persistentContainer.viewContext
            let context = managedObjectContext
            context.delete(tuArray[indexPath.row] as NSManagedObject)
            tuArray.remove(at: indexPath.row)

            do {
                try context.save()
            } catch _ {
            }

            // remueve el item eliminado del UITableView
            self.tableView.deleteRows(at: [indexPath], with: .fade) default:
            return

        }

    }

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.