My problem is that I can easily add and delete data but when I am trying to update data in Tableview did select method, when I update the last row it works fine but when I am trying to update any other row it will set the value to below row data. If any one have an answer for that then please help me for that.
import UIKit
import CoreData
class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var people = [Person]()
override func viewDidLoad() {
super.viewDidLoad()
let fetchRequest: NSFetchRequest<Person> = Person.fetchRequest()
do {
let people = try PersistenceServce.context.fetch(fetchRequest)
self.people = people
self.tableView.reloadData()
} catch {}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func onPlusTapped() {
let alert = UIAlertController(title: "Add Person", message: nil, preferredStyle: .alert)
alert.addTextField { (textField) in
textField.placeholder = "Name"
}
alert.addTextField { (textField) in
textField.placeholder = "Age"
textField.keyboardType = .numberPad
}
let action = UIAlertAction(title: "Post", style: .default) { (_) in
let name = alert.textFields!.first!.text!
let age = alert.textFields!.last!.text!
let person = Person(context: PersistenceServce.context)
person.name = name
person.age = Int16(age)!
PersistenceServce.saveContext()
self.people.append(person)
self.tableView.reloadData()
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .subtitle, reuseIdentifier: nil)
cell.textLabel?.text = people[indexPath.row].name
cell.detailTextLabel?.text = String(people[indexPath.row].age)
return cell
}
func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
return true
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let person = people[indexPath.row]
if editingStyle == .delete {
PersistenceServce.context.delete(person)
PersistenceServce.saveContext()
people.remove(at: indexPath.row)
self.tableView.deleteRows(at: [indexPath], with: .automatic)
}
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let alert = UIAlertController(title: "Update Person", message: nil, preferredStyle: .alert)
alert.addTextField { (textField) in
textField.text = self.people[indexPath.row].name
}
alert.addTextField { (textField) in
textField.text = String(self.people[indexPath.row].age)
textField.keyboardType = .numberPad
}
let action = UIAlertAction(title: "Update", style: .default) { (_) in
let name = alert.textFields!.first!.text!
let age = alert.textFields!.last!.text!
print(self.people)
PersistenceServce.context.delete(self.people[indexPath.row])
print(self.people)
let person = Person(context: PersistenceServce.context)
person.name = name
person.age = Int16(age)!
self.people.append(person)
self.people.remove(at: indexPath.row)
print(self.people)
self.tableView.reloadRows(at: [indexPath], with: .automatic)
PersistenceServce.saveContext()
}
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
}