Answer the question
In order to leave comments, you need to log in
Working with CoreData, how to update a cell?
Good afternoon/evening,
please help: How to update a cell in CoreData
The task is this: I have a list in the form of a UITableRow and they are either added to the list, the second attribute of the entity is responsible for the list. if not in the list, then false, and vice versa,
in general, I have this code, but I can’t understand how to update, I googled, googled, but I don’t have enough knowledge to understand
// Проверяем добавленн уже в список продукт или нет
// Обновляем CoreData со ячейкой
let row = self.test[indexPath.row].valueForKey("productName")!
let managedObjectContext = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext
let fetchRequest = NSFetchRequest(entityName: "Products")
if let fetchResults = try!managedObjectContext.executeFetchRequest(fetchRequest) as? [NSManagedObject] {
for i in 0..<fetchResults.count {
if String(fetchResults[i].valueForKey("productName")!) == row as! String {
// Обновляем наши данные
//managedObjectContext.setValue(1, forKey: "addedToList")
if (fetchResults[i].valueForKey("addedToList")!.boolValue == true) {
managedObjectContext.setValue(0, forKey: "addedToList")
} else {
managedObjectContext.setValue(1, forKey: "addedToList")
}
try!managedObjectContext.save()
self.tableView.reloadData()
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question