【问题标题】:UITableView with Xcode, Fetching CoreData ProblemUITableView 与 Xcode,获取 CoreData 问题
【发布时间】:2022-01-14 00:05:54
【问题描述】:

我一直在为一个项目工作,但坚持如何将 CoreData 检索到我的表视图。这是我的代码:

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

func loadData(){
       let requestData: NSFetchRequest<Person> = Person.fetchRequest()
       do {
           try
               personArr = context.fetch(requestData)
               try context.save()
          } catch {print ("Error retrieving data request \(error)")
           self.tableView.reloadData()
       }
   }

//  let DVC = segue.destination as! addPersonController
// numberOfRowsInSection ... cellForRowAt
// let cell = tableView.dequeueReusableCell(withIdentifier: "personCell", for: indexPath)
// cell.textLabel?.text = itemArray[indexPath.row].title > let person = personArr[indexPath.row]

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
      if(tableView.cellForRow(at: indexPath)?.accessoryType == .checkmark)
       {tableView.cellForRow(at: indexPath)?.accessoryType = .none
           personArr[indexPath.row].finish = false
           //cell.accessoryType = .checkmark //forRaw
       }
        tableView.deselectRow(at: indexPath, animated: true) //not selected
        savePerson()
       // let myPerson = PersonArr[indexPath.row].name
       // performSegue(withIdentifier: "personSegue", sender: myPerson)
}

这只是我应用的一些函数的一个例子。我担心的是将数据提取到未按预期提取的 personArr 中。有什么想法吗?

【问题讨论】:

    标签: swift uitableview


    【解决方案1】:

    仔细查看代码会发现您正在catch 范围内重新加载表格视图,这几乎永远不会发生。

    将该行移入do范围并删除无意义的行以保存上下文

    func loadData(){
       let requestData: NSFetchRequest<Person> = Person.fetchRequest()
       do {
           personArr = try context.fetch(requestData)
           self.tableView.reloadData()
       } catch {
           print ("Error retrieving data request \(error)")
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2013-09-20
      相关资源
      最近更新 更多