Delete a Tableview + Coredata line

Asked

Viewed 273 times

0

I am creating an application with tables and persistence with CoreData, but when I ask to erase a line the same does not disappear completely from the screen, leaving the symbol...disappearing only when I go to a second screen and return.

override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {


    let managedObject : NSManagedObject = frc.objectAtIndexPath(indexPath) as! NSManagedObject
    moc.deleteObject(managedObject)



    do{
        try moc.save()
    }catch{
        print("Error, data not saved!")
    }

        self.tableView.reloadData()


}

Primeira tela antes de deletar Depois de deletar

  • decrease the size of these images there and post the code you are using to delete, from a read in this article here when you can:http://answall.com/help/mcve

  • Thanks Gabriel, I’m not with my Mac now, later put the code part.

  • Could someone help me?

1 answer

2

I advise you to start using Nsfetchedresultcontroller. This is perfect for dealing with line editing + coredata on a Tableview.

Let me describe a basic step-by-step of what you should do to get started Nsfetchedresultcontroller:

  1. Make your class inherit from NSFetchedResultsControllerDelegate;
  2. Then create a variable as follows:

    lazy var fetchedResultsController: NSFetchedResultsController = {
        let fetchRequest = NSFetchRequest(entityName: "NomeDaSuaEntidade")
        fetchRequest.sortDescriptors = [NSSortDescriptor(key: "atributoParaOrdenar", ascending: true)]
        let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.sharedContext, sectionNameKeyPath: nil, cacheName: nil)
        return fetchedResultsController
    }()
    
  3. In the viewDidLoad add the following code snippet:

    ...
    do {
        try fetchedResultsController.performFetch()
    } catch {}
    
    fetchedResultsController.delegate = self
    ...
    
  4. To fill the table in the method cellForRowAtIndePath recover the objects as follows:

    ...
    let objeto = fetchedResultsController.objectAtIndexPath(indexPath) as! NomeDoSeuObjeto
    ...
    
  5. In the method numberOfRowsInSection recover the number of items as follows:

    ...
    let sectionInfo = self.fetchedResultsController.sections![section]
    return sectionInfo.numberOfObjects
    ...
    
  6. Now the great charm of Nsfetchedresultcontroller which makes it so worth using that delegate the method that takes care of table inserts, removals, updates and line drives:

    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
        switch type {
        case .Insert:
            self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
        case .Delete:
            self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
        case .Update:
            let cell = tableView.cellForRowAtIndexPath(indexPath!) as! ArtistCell
            let artist = controller.objectAtIndexPath(indexPath!) as! NomeDoSeuObjeto
            //preenche celula com dados do objeto
        case .Move:
            self.tableView.deleteRowsAtIndexPaths([indexPath!], withRowAnimation: .Fade)
            self.tableView.insertRowsAtIndexPaths([newIndexPath!], withRowAnimation: .Fade)
        }
    }
    
  7. To finish add the following methods so that everything works perfectly:

    func controllerWillChangeContent(controller: NSFetchedResultsController) {
        self.tableView.beginUpdates()
    }
    
    func controllerDidChangeContent(controller: NSFetchedResultsController) {
        self.tableView.endUpdates()
    }
    

By following the above steps you will be able to make the removal of a cell work as well as be able to take advantage of other benefits that this delegate has to offer.

I will also give you the link of a very good tutorial showing how to do to complement what I said above: http://code.tutsplus.com/tutorials/core-data-and-swift-nsfetchedresultscontroller--cms-25072 (I know the tutorial is in English, but I did not find any good in Portuguese)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.