简体   繁体   English

使用存储在核心数据中的数据将数据从表View Controller传递到View Controller

[英]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.

相关问题 将数据从表视图传递到视图控制器 - Pass data from table view to view controller 使用核心数据从表格视图打开编辑视图控制器 - Open edit view controller from table view using core data 将核心数据从选定的表格单元格传递到新的视图控制器 - Pass core data from selected table cell to new view controller 如何将视图 controller 中的数据传递到表视图 controller? - How to pass the data from view controller to the table view controller? 使用委托将数据从Collection View Controller传递到View Controller - Pass data from Collection View Controller to View Controller using delegates 在同一视图 controller 中将数据从集合视图传递到表视图: - pass data from collection view to table view in the same view controller: 将数据从tableview控制器传递到明细表view控制器 - pass data from tableview controller to details table view controller 如何将核心数据信息从表视图传递到视图控制器? - How can I pass core data information from a table view to a view controller? 如何将数据从表视图传递到另一个视图 controller - How to pass the data from table view to another view controller 如何将不同的数据从表视图传递到另一个视图 controller - How to pass the different data from table view to another view controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM