0

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.

This is the file that controls the 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.

This is the detailViewController code :

import UIKit

class costumerDetailViewController: UIViewController {
    var getCostumerName = String()

    @IBOutlet weak var labelName: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        labelName.text! = getCostumerName
    }
}
1
  • why are you creating String object "var getCostumerName = String()" in second controller? Just declare it as a type "var getCostumerName:String?" . Commented Dec 6, 2017 at 3:40

1 Answer 1

1

First Check Cn has value or "" on this line.

let Cn = n.costumerName

Change your code in class costumerDetailViewController for declare getCostumerName

var getCostumerName = "" //for avoid crash. if value goes nil.

Use Print() in viewDidLoad and debug it.

Hope this will help you.

Sign up to request clarification or add additional context in comments.

3 Comments

when I change var getCostumerName:String? because Xcode say add ! in this line labelName.text! = getCostumerName! I get this error Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
Did you print DvC.getCostumerName, it has value or nil?
any value I don't know why , I try print in console to se the value and don't show any value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.