1
votes

I cannot transfer an image and text from a tableView to another imageView using prepareForSegue method.

foodimages is an array holding imageNames of type string.

I have also tried:

let imgName = "image\(index).png"

but failed.

import UIKit

class TableView: UITableViewController,UITableViewDataSource,UITableViewDelegate {

var foodNames: [String] = ["Food1","Food2","Food3","Food4","Food5","Food6","Food7","Food8"];
var foodImages: [String] = ["image1", "image2","image3","image4","image5","image6","image7","image8"];


override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCellWithIdentifier("tblFoto", forIndexPath: indexPath) as! UITableViewCell

    cell.textLabel!.text = foodNames[indexPath.row]
    var image : UIImage = UIImage(named: foodImages[indexPath.row])!
    cell.imageView!.image = image

    return cell
}



override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {

    tableView.deselectRowAtIndexPath(indexPath, animated: true)
    self.performSegueWithIdentifier("showIMG", sender: indexPath)

}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
       if (segue.identifier == "showIMG") {
        var destViewController: imgView = segue.destinationViewController as! imgView

        var index = self.tableView.indexPathForSelectedRow()!.row

            let imgName = foodImages[index]        //"image\(index).png"
            let data = UIImage(named: imgName)!
            destViewController.photo = data
    }
}

Second Class named "imgView" is

import UIKit

class imgView: UIViewController {

@IBOutlet var secView: UIImageView!
var photo: UIImage!

override func viewDidLoad() {
    secView.image = photo
}   

}

In debug area it gives:

index (Int)7
imgName (String) "KLWR"
data (UIImage) 0x000000010d667e10 0x000000010d667e10

1
Are you sure that the name you are passing here 'UIImage(named: imgName)!' is present in your bundle? as this method 'UIImage(named:' always fetch the image name from the bundle.. - Vizllx
is photo an UIImage as well? how do you detect that the image is in fact not transfered? - luk2302
Strange thing to me is that after using deselectRowAtIndexPath, self.tableView.indexPathForSelectedRow() is still returning a value. - Vasil Garov
i do not know what's the matter with this btw i have uploaded my complete code, kindly review it Thank you Vizllx, luk2302 and flash advanced - user3314286
yes, it is returning a value. @flashadvanced - user3314286

1 Answers

0
votes

What I think is happening here is that you try to assign the UIImage in viewDidLoad and it is called before you assign the image in the calling view controller.

What you can do is something like this:

var image: UIImage? {
    didSet {
        self.secView.image = image
    }
}

So you expose an image property in the destination view controller and when the image is set then you update your image view.