S
S
Stanislav Korolevskiy2015-12-15 16:55:32
Swift
Stanislav Korolevskiy, 2015-12-15 16:55:32

Delete cell method not working. Where is the mistake?

Good afternoon! I'm trying to set up a button to delete data from a cell. Does not work...

//добавляю действие удаления для ячейки
func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        
        if editingStyle == .Delete {
            let itemToDelete = items.count![indexPath.row] as! UITableView
            NSManagedObjectContext.deleteObject(itemToDelete)
            
            do {
                try NSManagedObjectContext.save()
                tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Automatic)
                
            }   catch let error as NSError {
                print("Error \(error.localizedDescription)")
            }
            
        }
        
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Stanislav Korolevskiy, 2015-12-15
@korolevsky_s

Maybe someone will be interested. Solved the issue through the switch design.

func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        
        switch editingStyle {
        case .Delete:
   
            let appDel:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
            let context:NSManagedObjectContext = appDel.managedObjectContext
            context.deleteObject(items[indexPath.row] as NSManagedObject)
            items.removeAtIndex(indexPath.row)
            do{
                try context.save()
            }catch{
                print("Error, data not saved!")
            }

            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        default:
            return
            
        }
    }

K
Karetski, 2015-12-21
@Karetski

In general, it's better to read about NSFetchedResultsController about this.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question