डुप्लिकेट कोशिकाओं में से प्रत्येक पर लोड के tableView से coredata

0

सवाल

देखने से पता चलता है डुप्लिकेट पंक्तियों में प्रत्येक रिकॉर्ड के लिए CoreData जो बढ़ रखने के लिए प्रत्येक पुनः लोड. कोड निम्नानुसार है । क्या होता है जब भी मैं रिकॉर्ड जोड़ें तो मैं देखने के रिकॉर्ड यह मुझे पता चलता है रिकॉर्ड. तो मैं वापस क्लिक करें मुखपृष्ठ के लिए उस के बाद जब मैं क्लिक करें पर दृश्य रिकॉर्ड मैं नकल का एक ही रिकॉर्ड है । तो अब मेरे पास है 2 एक ही रिकॉर्ड है । कर सकते हैं किसी के साथ मेरी मदद करो और मुझे लगता है कि समस्या है, तालिका में देखें तो यहाँ है मेरे तालिका दृश्य नियंत्रक कोड

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

अपने कोड अविश्वसनीय है बोझिल है.

  • सब से पहले कभी नहीं की घोषणा के एक डेटा स्रोत के बाहर किसी भी वर्ग.
  • दूसरे के सभी कभी नहीं का उपयोग करें एक समारोह का निर्माण करने के लिए एक सरणी के रूप में तालिका डेटा को देखने स्रोत है ।
  • तीसरे के सभी firstRun व्यर्थ है क्योंकि viewDidLoad कहा जाता है केवल एक बार वैसे भी.
  • चौथी के बजाय छानने प्राप्त रिकॉर्ड मैन्युअल रूप से लागू करने के लिए विधेय लाने का अनुरोध

आगे यह अत्यधिक की सिफारिश की है नाम करने के लिए कोर डेटा संस्थाओं में हमेशा एकवचन रूप (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ý
..................................................................................................................

इस श्रेणी में लोकप्रिय

लोकप्रिय सवाल इस श्रेणी में