Pannu
Pannu

Reputation: 96

UIViewController when UITableViewCell is Pressed

I am trying to make an app so that when the cell in the UITableView is pressed, it opens a new ViewController with the same image and label used before in the initial cell that was clicked.

Here are my cells: My Cells Here is the array I am using:

let alligator:NSArray = ["American Alligator","American Crocodile","Mugger Crocodile","Saltwater Crocodile"]

Here is the numberOfRowsInSection and cellForRowAt:

 public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return(alligator.count)
}


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell4 = tableView.dequeueReusableCell(withIdentifier: "cell4", for: indexPath) as! AlligatorViewCell
    cell4.alligatorImage.image = UIImage.init(named: alligator[indexPath.row] as! String + ".jpg")
    cell4.alligatorLabel.text = alligator[indexPath.row] as? String
    return(cell4)
}

Here are my outlets for the detail View Controller:

@IBOutlet var detailImage: UIImageView! @IBOutlet var detailLabel: UILabel!

And here is the didSelectRowAt

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    detailImage.image = UIImage.init(named: alligator[indexPath.row] as! String + ".jpg")!
    detailLabel.text = alligator[indexPath.row] as? String

The problem is that when I press the cell, I get the error, fatal error: Unexpectedly found nil while Unwrapping Optional

Upvotes: 0

Views: 75

Answers (1)

Benjamin Lowry
Benjamin Lowry

Reputation: 3799

I believe your error is occurring because detailImage does not exist when you call it.

From what I understand, these outlets are in your base view controller, but they are referring to objects in your detail view controller, which isn't allowed. Thus when you try and call them, they won't pass anything giving a value of nil.

Thus it is not about the UIImage being initialized.

You need to make the IBOutlets within the detailViewController class, and then pass the values from your cell to these outlets through your prepareForSegue func.

Let me know if you have questions about this.

Edit

Let's say your detail view controller has the detailImage property, then you could do something like this (in your base ViewController)

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let controller = segue.destination as! UIViewController //your detail controller
    controller.detailImage = myImage //where myImage is a property of your base view controller
}

Edit

You could set the myImage property in the view controller when your cell is tapped just as your tried before. Then myImage could be set to the detailImage in the prepareForSegue func. So something like this:

public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myImage = UIImage.init(named: alligator[indexPath.row] as! String + ".jpg")!
    ...

Edit

You should be using myImage like this:

var myImage: UIImage?
public func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        myImage = UIImage.init(named: alligator[indexPath.row] as! String + ".jpg")!
        ...

Upvotes: 1

Related Questions