Answer the question
In order to leave comments, you need to log in
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
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
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question