Duplicitní buňky na každé zatížení tableView z coredata

0

Otázka

Zobrazení ukazuje duplicitní řádky pro každý záznam v CoreData, které se množí na každý jiný. Kód je tak pod. Co se stane, je, když jsem přidat záznam pak jsem prohlížení záznamu ukazuje mi záznamu. Pak jsem klepněte na tlačítko zpět na domovskou stránku po tom, když jsem klikněte na zobrazit záznam jsem viděl kopii stejný záznam. Takže teď mám 2 stejné záznamy. Může někdo prosím, pomozte mi a myslím, že problém je v zobrazení tabulky, tak tady je moje tabulka pohled řadič kód

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

Váš kód je neuvěřitelné těžkopádné.

  • Za prvé, nikdy deklarovat datové zdroje mimo jakékoliv třídy.
  • Za druhé nikdy použít funkci vytvořit pole jako tabulka, zobrazení dat zdroj.
  • Třetina všech firstRun je zbytečné, protože viewDidLoad je volána pouze jednou tak jako tak.
  • Čtvrtý všech, spíše než filtrování přijaté záznamy ručně aplikovat predikát pro načtení žádosti

Dále je to vysoce doporučeno, aby jméno Základních Údajů subjektů vždy v jednotném čísle (Record) a používat konkrétní generic přinést žádost tohoto subjektu.

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

V jiných jazycích

Tato stránka je v jiných jazycích

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