เซลล์ที่ซ้ำกันบนกันและโหลดของ tableView จาก coredata

0

คำถาม

มุมมองแสดงเลเยอร์ถัดไปแถวสำหรับแต่ละประวัติอยู่ใน 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)
        }
    }
}
core-data duplicates ios swift
2021-10-23 03:50:16
1
0

รหัสของคุณคือไม่อยากจะเชื่อ cumbersome.

  • อย่างแรกไม่เคยประกาศข้อมูลแหล่งข่าวข้างนอกของชั้นเรียน
  • ครั้งที่สองจากทั้งหมดไม่เคยใช้ฟังก์ชันเพื่อสร้างเป็นอาเรย์ที่โต๊ะมุมมองข้อมูลแหล่งข่าว
  • ครั้งที่สามของทุก firstRun มันเปล่าประโยชน์เพราะ viewDidLoad เรียกว่าเพียงครั้งเดียวอยู่แล้ว
  • ที่สี่ของมากกว่ากำลังกรองที่ได้รับบันทึก ด้วยตนเอง ต้องปรับใช้ predicate เพื่อข้อมูลการร้องขอ

ต่อมันเป็นขอแนะนำให้ชื่อของแข็งข้อมูล entities เสมอยู่ใน singular รูปแบบ(Record)และเพื่อใช้ระบุทั่วไปอข้อมูลการร้องของของรายการ.

class TableViewController: UITableViewController {
    var records = [Record]()
   
    override func viewDidLoad() {
        super.viewDidLoad()
       
        let appDelegate = UIApplication.shared.delegate as! AppDelegate
        let context = appDelegate.persistentContainer.viewContext

        let request : NSFetchRequest<Record> = Record.fetchRequest()
        request.predicate = NSPredicate(format: "del != nil")
        do {
             records = try context.fetch(request)
        } catch { print("Fetch Failed", error) }
    }        
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "myCell") as! TableViewCell
        
        let thisrec = records[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 {
        return records.count
    }

 ...
2021-10-23 04:44:59

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่