มุมมองแสดงเลเยอร์ถัดไปแถวสำหรับแต่ละประวัติอยู่ใน CoreData ซึ่งทำให้ multiplying บกันและโหลดใหม่. กฎมันเป็นขั้นตอนนี้ สิ่งที่เกิดขึ้นได้คือเมื่อไหร่ก็ตามที่ฉันเพิ่มสถิติแล้วผมองมุมมองบันทึกมันแสดงให้เห็นการบันทึกสถิติ งั้นฉันคลิกย้อนกลับสำหรับหน้าเว็บหลักหลังจากนั้นตอนที่ฉันคลิกบนมุมมองบันทึกฉันเห็นคนคัดสำเนาของเดียวกับประวัติ ดังนั้นตอนนี้ฉันต้อง 2 คนเดียวกันมีการบันทึกไว้ มีใครได้โปรดช่วยฉันด้วยกันและฉันคิดว่าปัญหามันอยู่โต๊ะมุมมองดังนั้นนี่คือโต๊ะของผมองมุมมอง controller รหัส
import UIKit
import CoreData
var Rec = [Records]()
class TableViewController: UITableViewController {
var firstLoad = true
func nondel() -> [Records]
{
var nodellist = [Records]()
for note in Rec
{
if(note.del == nil)
{
nodellist.append(note)
}
}
return nodellist
}
override func viewDidLoad() {
super.viewDidLoad()
if(firstLoad)
{
firstLoad = false
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context:NSManagedObjectContext = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Records")
do{
let results: NSArray = try context.fetch(request) as NSArray
for result in results {
let note = result as! Records
Rec.append(note)
}
}
catch
{
print("Fetch Failed")
}
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! TableViewCell
let thisrec: Records!
thisrec = nondel()[indexPath.row]
cell.idLB.text = thisrec.id
cell.nameLB.text = thisrec.name
cell.lastLB.text = thisrec.last
cell.genderLB.text = thisrec.gender
cell.ageLB.text = thisrec.age
cell.addressLB.text = thisrec.address
return cell
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return nondel().count
}
override func viewDidAppear(_ animated: Bool) {
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
self.performSegue(withIdentifier: "editNote", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if(segue.identifier == "editNote")
{
let indexPath = tableView.indexPathForSelectedRow!
let recDetail = segue.destination as? AddViewController
let selectedCell: Records!
selectedCell = nondel()[indexPath.row]
recDetail!.selectedCell = selectedCell
tableView.deselectRow(at: indexPath, animated: true)
}
}
}