-1

I have a tableview and I want to show cell details in another view controller.

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let selectedCell = listAthkar[indexPath.row]
    let destinationVC = showCellVC()
    destinationVC.cellTitle.text = selectedCell.title
    destinationVC.cellDisc.text  = selectedCell.details
    performSegue(withIdentifier: "showCell", sender: self)
}

showCellVC has a UILabel and a textview which I want to pass data to, the data are coming from core data. The app crashes every time I press in a cell.

Here is the error I get

fatal error: unexpectedly found nil while unwrapping an Optional value 2017-08-27 02:46:29.315056-0400 AthkarKF[13152:3972483] fatal error: unexpectedly found nil while unwrapping an Optional value

The error I think is self-explanatory, but I'm not sure where is the optional value and I'm not sure if this is the correct way to pass data to another VC.

Can you please help, I would really appreciate it.

Imad Ali
  • 3,261
  • 1
  • 25
  • 33
Khalid
  • 3
  • 5
  • 2
    https://stackoverflow.com/questions/32170456/what-does-fatal-error-unexpectedly-found-nil-while-unwrapping-an-optional-valu https://stackoverflow.com/questions/29734954/how-do-you-share-data-between-view-controllers-and-other-objects-in-swift . Welcome to SO. Khalid, please make sure you search for threads before you ask the questions. If it does not help, you can post the question and tell us why the solutions did not work and it will be easier to help you , rather than posting duplicates. :) GL. –  Aug 27 '17 at 07:00
  • 2
    You've to pass the data in `prepareForSegue` method not in `tableView:didSelectRow`, Show your `prepareForSegue` method – Imad Ali Aug 27 '17 at 07:01

1 Answers1

0

What you should do is to pass the desired data through prepareForSegue:sender: method. You could achive this by doing the following:

1- Declare selectedCell as an instance variable to be accessible in the whole view controller:

// for sure you'll need to declare its data type...
var selectedCell:...

2- Remove "passing data" code from tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) delegate method, all you have to do is to perform the segue:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    selectedCell = listAthkar[indexPath.row]
    performSegue(withIdentifier: "showCell", sender: self)
}

3- Implement prepareForSegue:sender: and pass the data through it:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    // you may need to check the segue identifier in case of your view controller contains multiple segues
    if segue.identifier == "showCell" {
        let destinationVC = segue.destination as! showCellVC()
        destinationVC.cellTitle.text = selectedCell.title
        destinationVC.cellDisc.text  = selectedCell.details
    }
}


ّIn general, the final result should be similar to:

class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {

    // STEP #01:
    // for sure you'll need to declare its data type...
    var selectedCell:...

    // STEP #02:
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        selectedCell = listAthkar[indexPath.row]
        performSegue(withIdentifier: "showCell", sender: self)
    }

    // STEP #03:
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // you may need to check the segue identifier in case of your view controller contains multiple segues
        if segue.identifier == "showCell" {
            let destinationVC = segue.destination as! showCellVC()
            destinationVC.cellTitle.text = selectedCell.title
            destinationVC.cellDisc.text  = selectedCell.details
        }
    }
}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • let destinationVC = segue.destination as! showCellVC() 'as! showCellVC' or 'as! showCellVC()'? –  Aug 27 '17 at 08:38
  • @OmniRingo I didn't realize what's the difference. Anyway, assuming that `showCellVC()` should returns a *valid* UIViewController, it should be `let destinationVC = segue.destination as! showCellVC()`. – Ahmad F Aug 27 '17 at 09:09