【问题标题】:Edit core data through table view通过表格视图编辑核心数据
【发布时间】:2026-01-09 07:10:01
【问题描述】:

我的应用有两个视图控制器 - ViewController 和 ListViewController。 View Controller 上有一个要完成的表单,ListView 控制器提供所有已完成表单的表格视图。我正处于可以输入数据、删除数据的地步,但我在编码编辑功能方面遇到了困难。注意我的 for 是使用 Eureka 创建的。下面是将信息加载到核心数据中的代码。

我继续尝试寻找答案。我看过很多 YouTube 视频,但它们似乎过时了,我无法映射到新版本的 swift。

尝试使用 segue:在代码的最后一行出现错误“Type 'OTSProcessConfirmations.Type' has no subscript members”

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if segue.identifier == "editOTS"
    {
        let v = segue.destination as! ViewController

        let indexpath = self.tableView.indexPathForSelectedRow
        let row = indexpath?.row

        v.PC = OTSProcessConfirmations[row!]

    }

var branch: String = ""
var enterDate: Date? = nil
var OTSQ1: Bool = false
var OTSQ2: Bool = false
var OTSQ3: Bool = false
var OTSQ4: Bool = false
var OTSQ5: Bool = false
var OTSQ6: Bool = false
var OTSQ7: Bool = false
var OTSQ8: Bool = false
var OTSQ9: Bool = false
var OTSQ10: Bool = false
var OTSQ11: Bool = false
var OTSQ12: Bool = false
var OTSQ13: Bool = false
var OTSQ14: Bool = false

var processConfirmation: OTSProcessConfirmations?

@IBAction func BtnSave(_ sender: Any) {



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

    let ots = OTSProcessConfirmations(context:context)

    ots.branch = branch
    ots.enterDate = enterDate
    ots.otsq1 = OTSQ1
    ots.otsq2 = OTSQ2
    ots.otsq3 = OTSQ3
    ots.otsq4 = OTSQ4
    ots.otsq5 = OTSQ5
    ots.otsq6 = OTSQ6
    ots.otsq7 = OTSQ7
    ots.otsq8 = OTSQ8
    ots.otsq9 = OTSQ9
    ots.otsq10 = OTSQ10
    ots.otsq11 = OTSQ11
    ots.otsq12 = OTSQ12
    ots.otsq13 = OTSQ13
    ots.otsq14 = OTSQ14


    // Save the data to coredata

    (UIApplication.shared.delegate as! AppDelegate).saveContext()

    navigationController!.popViewController(animated: true)
}

ListView 控制器代码

import UIKit

class ListViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{

    @IBOutlet weak var tableView: UITableView!

    var OTSs : [OTSProcessConfirmations] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        tableView.dataSource = self
        tableView.delegate = self


        // Do any additional setup after loading the view.
    }

    override func viewWillAppear(_ animated: Bool) {
        // get the data from core data

        getData()


        /// reload the table view

        tableView.reloadData()
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        <#code#>
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return OTSs.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell()

        let ots = OTSs[indexPath.row]

        cell.textLabel?.text = ots.branch!


        return cell
    }
    func getData() {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

        do {
            OTSs = try context.fetch(OTSProcessConfirmations.fetchRequest())

        }
        catch{
            print("Fetching Failed")
        }
    }

    func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
        let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
        if editingStyle == .delete {

            let ots = OTSs[indexPath.row]
            context.delete(ots)

            (UIApplication.shared.delegate as! AppDelegate).saveContext()

            do {
                OTSs = try context.fetch(OTSProcessConfirmations.fetchRequest())
            }
            catch {
                print("Fetching Failed")
            }
        }
        tableView.reloadData()
    }

 }

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    错误很明显。

    您必须从数据源数组中获取所选行的实例,而不是核心数据实体OTSProcessConfirmations

    v.PC = OTSs[row!]
    

    顺便说一句,您的代码很难阅读。请遵守类和结构名称以大写字母开头,函数和变量名称以小写字母开头的命名准则。这样可以避免此类错误。

    此外,请使用比单个字母或缩写更具描述性的名称,尤其是在您要共享代码时。

    最后,类、结构和核心数据实体应该以单数形式命名 (OTSProcessConfirmation)。

    【讨论】:

    • 感谢您的反馈。我将重新启动我的项目并努力遵循正确的命名准则。有没有人可以推荐的新手参考?
    • 最全面的参考是Swift Language Guide