how can I get the image from dynamically generated UITableViewCell and pass it with segue to the other View?

I have a UITableViewController with UITableViewCell dynamically generated there. Each cell contains an imageView that I'm filling with images fetched from my server. I'm using alamofire_images to do so. My code looks as follows:

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

    let test:SingleTest =  self.items[indexPath.row] as! SingleTest

    if(test.photo != "") {
        cell.myPhoto.af_setImageWithURL(NSURL(string: test.photo)!)   
    } else {
        cell.myPhoto.image = UIImage(named: "clusterLarge")
    }
    return cell
}

I thought that since I'm downloading images while displaying the table, there is no need to download it again on the other screen (which is accessible through clicking each cell).

So my idea was to pass the image from specific cell to the other screen through segue. But the problem is that from the method prepareForSegue I don't have access to the specific cell that user clicks. So my other choice was to use protocols. I created a very simple one:

protocol HandlePhoto: class {
    func setUpBackgroundPhoto(miniature: UIImage)
}

and then in my native class I wanted to use it in didSelectRowAtIndexPath method:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
    let test:SingleTest =  self.items[indexPath.row] as! SingleTest

    let cell = testDetailsPanel.dequeueReusableCellWithIdentifier("cell") as! TestDetailsCell

    if(test.photo != "") {
            handlePhoto.setUpBackgroundPhoto(cell.testPhoto.image!)
            self.performSegueWithIdentifier("testPhotoDetailsSegue", sender: test)
        } 
    } else {
        self.performSegueWithIdentifier("testTextDetailsSegue", sender: test)
    }

}

But this line:

handlePhoto.setUpBackgroundPhoto(cell.testPhoto.image!)

throws error:

fatal error: unexpectedly found nil while unwrapping an Optional value

So my final question is: how can I access photo from the specific cell that user chooses and pass it to other view (without downloading it there for the 2nd time)?

Answers


Your didSelectRowAtIndexPath implementation is wrong, with dequeueReusableCellWithIdentifier you are obtaining a new cell, not the selected cell. Try this:

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

    let selectedCell = tableView.cellForRow(at: indexPath) as! TestDetailsCell

    //this will return downloaded image or "clusterLarge"
    let image = selectedCell.myPhoto.image

    //
    //Make your check on image and extra setup
    //

    self.performSegueWithIdentifier("testPhotoDetailsSegue", sender: test)

}

Why are you using dequeueReusableCellWithIdentifier in your didSelectRowAtIndexPath?; instead, you should get the cell directly using:

let cell = yourTableView.cellForRowAtIndexPath(indexPath) as! TestDetailsCell
if let image = cell.testPhoto.image {
    print(image)//this is what you want.
}

Need Your Help

How to create a disparity map?

java image pixel disparity-mapping

Okay so I have implemented a stereo correspondence algorithm which takes a stereo image pair, matches a point on the left image with a point on the right image, and finds the disparity between the ...

Loading an object from the database in each request in Laravel using a Middleware, (Performance perspective)

performance laravel caching menu

I'm trying to implement a form in laravel which manages the menu items dynamically, and I found this great package: