ios - 更新表格 View 中的条目(编辑后)并在表格 View 中显示时出现问题

标签 ios swift core-data

这是我的场景...单击右上角(导航栏)的加号按钮时,会出现一个带有文本字段的警报 View ,我在文本字段中添加一些数据,然后按“确定”按钮。这会导致文本字段中的数据显示在 TableView 上,并且也存储在 core-data 中。

然后,当我单击现在包含警报 View 文本字段中的数据的行时,我会转到另一个要编辑的 View 。该viewcontroller有一个文本字段,其中包含前一屏幕行中的值。现在,我单击文本字段并编辑其值,然后按右上角的“保存”。现在,理想情况下,当我按“保存”并转到上一个屏幕时,编辑后的值现在应该在表格 View 上看到,而不是旧值。

但是发生的情况是,当我按“保存”并返回到上一个屏幕时,我暂时会在 tableviewcontroller 中看到更新后的值。但实际上,该值在 core-data 中添加了两次,当我从这个 tableview 返回到另一个 View 并返回到它时,我不仅看到编辑之前存在的值而且新编辑的值也被添加了两次!我无法理解这种行为......

单击编辑屏幕中的“保存”按钮后,这就是我正在做的事情...

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

返回 TableView 屏幕后,这就是我正在做的事情...(在 tableviewcontroller 屏幕中)

@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()

}

在 cellForRowAtIndexPath 中,这就是我正在做的事情......

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

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

最佳答案

您正在插入一条新记录而不是更新前一条记录
在类别实体上进行任何插入之前,编写一段代码来获取具有当前名称的记录,然后用编辑后的字符串替换其当前名称,然后保存它。
此代码获取旧名称的记录并将其名称替换为新名称

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{}

关于ios - 更新表格 View 中的条目(编辑后)并在表格 View 中显示时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46378250/

相关文章:

ios - 如何以用户可读的形式显示核心数据获取的数据

ios - 在 iOS 核心数据中映射嵌入式对象

ios - NSOrderedSet 不会构建在 XCode 生成的 iPhone 类中

ios - userInfoTransfer,有效负载太大

ios - 在 collectionview 上滚动后 dequeueReusableCellWithReuseIdentifier 出错了

ios - Swiftui 动态动画面板从下到上

ios - 如何防止在屏幕上通知用户有关 CloudKit 的新更改?

ios - 当我不注销就终止应用程序时,经过身份验证的用户是否保持登录状态?

ios - 异步图像下载期间滚动速度更快时崩溃

ios - 如何在 iOS 中获取大量电池信息?