0

There are many other topics of passing data to another ViewController - i know - but i could not find the solution for passing data from UITableViewCell to UIViewController. The question differs from others that here i have to access an ImageView in another class of UITableViewCell. Segues, prepare for segues an other topics are discussed in other posts sufficiently but not this special constellation.

I have a class UITableViewCell:

class PostCell: UITableViewCell {
...
// networkService is downloading an image 
networkService.downloadImage({ (imageData) in
            let image = UIImage(data: imageData as Data)
...
// image is set to UIImageView
self.postImageView.image = image

In my ViewController i do this to go to the DetailViewController:

 override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    aktIndex = indexPath.section

    performSegue(withIdentifier: "segueDetail", sender: self)
}

I tried this:

 let MainStory:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let desVC = MainStory.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController

    desVC.getImage = ???
    self.navigationController?.pushViewController(desVC, animated: true)

DetailViewController:

class DetailViewController: UIViewController {

var getImage = UIImage()
...

I have made a segue in xcode (segueDetail): enter image description here

At the moment i store the imagedata in UserDefaults and read them again in the DetailViewController. Very weird, i know.

Where do i have to pass the data from? In my PostCell or in the ViewController? The problem is to get access to image-data from PostCell in my ViewController.

Ralf Bordé
  • 333
  • 4
  • 18

1 Answers1

1

In tableView(_:didSelectRowAt:) when you call performSegue(withIdentifier: sender:), you can pass any data or references in it. This is now available in prepare(forSegue:sender:) and your last shot at preparing the data to be passed to the segued viewController.

Example (Using Segue):

If segueDetail is properly hooked up via storyboard and your user taps on a row, you could send the indexPath to the segue like:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    performSegue(withIdentifier: "segueDetail", sender: indexPath)true)
}

Then in prepare(forSegue:sender:), depending on your solution, you can prepare access to the required data that you need to pass to the next viewController like:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let vc = segue.destination as? DetailViewController {
        let indexPath = sender as! IndexPath

        let cell = tableView.cellForRow(at: indexPath) as! PostCell
        vc.getImage = cell.postImageView.image
    }
}

Example (Manually without segue):

If you are not using a segue and the user taps on a row, you could manually push a viewController with data like:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let sb = UIStoryboard(name: "Main", bundle: nil)
    let vc = sb.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController

    let cell = tableView.cellForRow(at: indexPath) as! PostCell
    vc.getImage = cell.postImageView.image

    self.navigationController?.pushViewController(desVC, animated: true)
}

And your DetailViewController should be:

class DetailViewController: UIViewController {    
    var getImage: UIImage?
    //...

NOTE: This answer is the best I could fit to work with the given content.
It should just about work but please don't just copy-paste as it's not optimized (for example, your user taps on a cell before it's image is downloaded).
This was just to show the basics, so please improvise and apply proper case handling.

Community
  • 1
  • 1
staticVoidMan
  • 19,275
  • 6
  • 69
  • 98
  • thx this is a great approach. I made a litte improvement but now the following error occurs: “View Controller“ is unreachable because it has no entry points, and no identifier for runtime access via -[UIStoryboard instantiateViewControllerWithIdentifier:]. [9]" - Why this ? – Ralf Bordé Mar 04 '19 at 19:29
  • @RalfBordé check ur storyboard. It shud have an initial `viewController`; [refer solution](https://stackoverflow.com/a/39847399/2857130) – staticVoidMan Mar 04 '19 at 21:05
  • my TabBarController is marked as my Initial ViewController. I will have a look at your refer solution. – Ralf Bordé Mar 05 '19 at 08:33
  • Problem solved! In my DetailViewController i gave a StoryboardID "SegueDetailViewController" and then put it in the code "...(withIdentifier: "SegueDetailViewController") as! DetailViewController". Now it works fine ! – Ralf Bordé Mar 05 '19 at 09:54
  • @RalfBordé Glad to help. Don't forget to mark answer as accepted or upvote ;) – staticVoidMan Mar 05 '19 at 10:38
  • Done, THX a lot! – Ralf Bordé Mar 05 '19 at 10:49