简体   繁体   中英

Issue with updating entries in tableview (after editing)and showing in Tableview

This is my scenario...On clicking a plus button on the top right (of the navigation bar), an alertview with textfield appears and I add some data in the textfield and press the OK button. This causes the data in the textfield to be shown on the tableview and it is also stored in core-data.

Then when I click on that row which now has the data from the alertview textfield, I go to another view for which is for editing. This viewcontroller has a textfield which has the value from the row of the previous screen. Now I click on the textfield and edit the value on it and press save at the top right. Now, ideally when I press save and go to the previous screen, the edited value should now be seen on the tableview instead of the old value.

But what is happening is when I press save and go back to the previous screen, I do for the time being see the updated value in the tableviewcontroller . But in actuality, that value is added twice in core-data and when I go back from this tableview to some another view and return back to it, then I see not only the value that was present before editing but also the new edited value being added twice!. I'm not able to understand this behaviour....

On clicking the Save button in the edit screen this is what I'm doing...

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "save" {
            editedModel = editTextField.text
    }
}

On coming back to the table view screen, this is what I'm doing...(in the tableviewcontroller screen)

@IBAction func saveToMainEditViewController (segue:UIStoryboardSegue) {
    let detailViewController = segue.source as! EditCategoriesTableViewController
    let index = detailViewController.index
    let modelString = detailViewController.editedModel //Edited model has the edited string

    let myCategory1 = Category(context: self.context)
    myCategory1.categoryName = modelString
    mangObjArr[index!] = myCategory1     

    //Saving to CoreData
    guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
        return
    }
    let managedContext = appDelegate.persistentContainer.viewContext
    let entity = NSEntityDescription.entity(forEntityName: "Category", in: managedContext)
    let category = NSManagedObject(entity: entity!, insertInto: managedContext)

    category.setValue(myCategory1.categoryName, forKeyPath: "categoryName")
    category.setValue(myCategory1.categoryId, forKey: "categoryId")
    do {

        try managedContext.save()
    } catch let error as NSError {
        print("Could not save. \(error), \(error.userInfo)")
    }
    categoryTableView.reloadData()

}

In the cellForRowAtIndexPath, this is what I am doing...

    let cell = tableView.dequeueReusableCell(withIdentifier: "categoryCell", for: indexPath)

    let person = mangObjArr[indexPath.row]
    cell.textLabel?.text = person.categoryName
    return cell

You are inserting a new record instead of update previous one
before any insertion on Category entity, write a piece of code that fetch a record with current name, then replace its current name with edited string and then save it.
this code fetch record with old name and replace its name with a new one

let fetchRequest : NSFetchRequest<Category> = Category.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "(categoryName == %@)", argumentArray: [*Your current categoryName*])
 do {
     let result = try managedContext.fetch(fetchRequest)
     if let toBeUpdatedRecord = result.first
     {
        toBeUpdatedRecord.categoryName = newName
        //here you should save 
     }
} catch{}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM