[英]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. 这是我的情况...单击导航栏右上角的加号按钮时,将出现一个带有文本字段的Alertview,然后在文本字段中添加一些数据,然后按OK按钮。 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. 然后,当我单击现在具有来自alertview文本字段的数据的行时,我转到另一个要编辑的视图。 This viewcontroller
has a textfield which has the value from the row of the previous screen. 该viewcontroller
具有一个文本字段,该字段具有上一屏幕行中的值。 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
. 但是发生的是,当我按保存并返回上一屏幕时,我暂时在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!. 但实际上,该值在核心数据添加两次,当我回去从这个tableview
一些其他视图,并返回到它,然后我看到不仅是编辑也是新编辑的值是之前存在的价值加了两次! 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) 回到表视图屏幕,这就是我正在做的...(在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()
}
In the cellForRowAtIndexPath, this is what I am doing... 在cellForRowAtIndexPath中,这就是我正在做的...
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. 在对Category实体进行任何插入之前,编写一段代码,以当前名称获取记录,然后用编辑后的字符串替换其当前名称,然后保存。
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{}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.