重复细胞上的每一个载荷,从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ý
..................................................................................................................