2017-10-20 30 views
0

Donc j'essaye de supprimer des données d'un tableView, qu'il effacera la cellule à la rangée, mais il ne supprimera pas l'information de coreData, l'amenant à charger encore quand J'appelle un .reloadData(). Je suis vraiment nouveau à Coredata et je ne sais pas comment sélectionner un article spécifique que je crée.Comment supprimer l'objet spécifique de base de données de TableView

Voici où je manipule la suppression:

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if (editingStyle == UITableViewCellEditingStyle.delete) { 
     // handle delete (by removing the data from your array and updating the tableview) 
     recipes.remove(at: indexPath.row) 
     tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 

    } 

} 

Voilà comment je crée les éléments CoreData, si cela aide. De plus, j'ai un dépôt Git here si quelqu'un est prêt à regarder cette profonde

@IBAction func createNewRecipeButton(_ sender: Any) { 
     let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext 
     let newRecipe = Recipe(context: context) 
     newRecipe.title = recipeTitleTextBox.text 
     newRecipe.instructions = recipeInstructionsTextBox.text 
     newRecipe.time = recipeTimeTextBox.text 
     (UIApplication.shared.delegate as! AppDelegate).saveContext() 
     navigationController!.popToRootViewController(animated: true) 
} 

Répondre

1

Votre méthode de suppression actuelle supprime simplement la recette de la baie de stockage. Vous devez dire le contexte de se débarrasser de lui aussi bien ...

func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) { 
    if (editingStyle == UITableViewCellEditingStyle.delete) { 
     let recipe = recipes.remove(at: indexPath.row) 
     tableView.deleteRows(at: [indexPath], with: UITableViewRowAnimation.automatic) 
     guard let moc = recipe.managedObjectContext else { return } 
     moc.delete(recipe) 
     moc.processPendingChanges() 
    } 
} 

Vous pouvez également regarder dans un NSFetchedResultsController. Il y a plusieurs tutoriels autour.