我的应用程序有两个 View Controller - ViewController 和 ListViewController。 View Controller 上有一个要完成的表单,ListView Controller 提供了所有已完成表单的 TableView 。我正处于可以输入数据、删除数据的地步,但我正在为编码编辑功能而苦苦挣扎。请注意,我的 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 Controller 代码
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()
}
}
最佳答案
错误很明显。
您必须从数据源数组中获取选定行的实例,而不是核心数据实体 OTSProcessConfirmations
。
v.PC = OTSs[row!]
顺便说一下,您的代码很难阅读。请遵循类和结构名称以大写字母开头,函数和变量名称以小写字母开头的命名准则。这样可以避免此类错误。
此外,请使用比单个字母或缩写更具描述性的名称,特别是如果您要共享代码。
最后,类、结构和核心数据实体应该以单数形式命名 (OTSProcessConfirmation
)。
关于ios - 通过 TableView 编辑核心数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47128877/