[英]pass data from a table view Controller to View Controller using data stored in core data
I'm a little newbie and I have a doubt, I have a TableViewController
and another ViewController
that I have as a detailViewController
, what I try to do is that when a cell is selected in the tableview
, it presents the corresponding data stored in core data for that cell in the detailViewcontroller
. 我是一个新手,我有一个疑问,我有一个
TableViewController
和另一个ViewController
作为detailViewController
,我尝试做的是当在tableview
选择一个单元格时,它会显示存储在core中的相应数据detailViewcontroller
该单元格的数据。
This is the file that controls the tableViewcontroller
: 这是控制
tableViewcontroller
的文件:
import UIKit
class CostumerTableViewController: UITableViewController {
var costumerArray:[Costumer] = []
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.reloadData()
self.fetchData()
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return costumerArray.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
let name = costumerArray[indexPath.row]
cell.textLabel?.text = name.costumerName!
return cell
}
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
if editingStyle == .delete {
let costumerDelete = costumerArray[indexPath.row]
context.delete(costumerDelete)
(UIApplication.shared.delegate as! AppDelegate).saveContext()
do {
costumerArray = try context.fetch(Costumer.fetchRequest())
} catch {
print(error)
}
}
tableView.reloadData()
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let Storyboard = UIStoryboard(name: "Main", bundle: nil)
let DvC = Storyboard.instantiateViewController(withIdentifier: "costumerDetailViewController") as! costumerDetailViewController
let n = costumerArray[indexPath.row]
let Cn = n.costumerName!
DvC.getCostumerName = Cn
self.navigationController?.pushViewController(DvC, animated: true)
}
func fetchData() {
// se crea el context
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
do { // se hace el request del array
costumerArray = try context.fetch(Costumer.fetchRequest())
} catch {
print(error)
}
}
}
In the compilation does not give me any problem, some everything goes well the problem is that it does not present anything in the detail viewController
label that in this case I try to send the data from this code. 在编译中不会给我任何问题,某些事情进展顺利,问题是它没有显示在细节
viewController
标签中的任何内容,在这种情况下,我尝试从此代码发送数据。
This is the detailViewController
code : 这是
detailViewController
代码:
import UIKit
class costumerDetailViewController: UIViewController {
var getCostumerName = String()
@IBOutlet weak var labelName: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
labelName.text! = getCostumerName
}
}
First Check Cn
has value or "" on this line. 首先检查
Cn
在此行上具有值或“”。
let Cn = n.costumerName
Change your code in class costumerDetailViewController
for declare getCostumerName
更改代码类
costumerDetailViewController
的申报getCostumerName
var getCostumerName = "" //for avoid crash. if value goes nil.
Use Print()
in viewDidLoad and debug it. 在viewDidLoad中使用
Print()
并对其进行调试。
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.