I
I
Ivan Vasilich2016-12-02 14:12:27
Swift
Ivan Vasilich, 2016-12-02 14:12:27

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

1 answer(s)
I
i_allar, 2016-12-04
@i_allar

look at which row class, it needs to write this product name into some text label. If it's a UITableViewCell try cell.textLable.text = fetchResults [i].valueForKey("productName"); if it is a descendant of UITableViewCell, look at its text label name

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question