0
votes

i recently made a tableview filled with cells. i was wondering if i can, when i tap a cell display a certain picture instead of a blank screen

i tried linking cells with different image views but i can't do that using a tableview ,so what is the required code

i do know that I'm supposed to use the prepareForSegue statement

override prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {}

i just don't know what to type

i only want to add an image display when i tap one of the cells

thanks for the help

2
So I assume you want to display the image of the cell in another UIViewController?Swift Rabbit

2 Answers

0
votes

You have to add another view controller in your storyboard which will be opened after tapping the cell.

In tableView(_:didSelectRowAtIndexPath:) you have to remember which cell was tapped, and in prepareForSegue you can pass data to the destination view controller. In destination view controller just load the image based on data passed.

0
votes

You can use prepareForSegue to pass data from your cell to the next ViewController (the image one).

I would do something similar to this.

override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    self.indexTapped = indexPath.row
    self.performSegueWithIdentifier("goToImageViewController", sender: self)
}

....

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if (segue.identifier == "goToImageViewController") {
        var vc = segue.destinationViewController as! ImageViewController
        vc.image = imageDatasource[indexTapped] as! UIImage
    }
}

Once you call performSegueWithIdentifier(identifier: String?, sender: AnyObject?) the transition will be automatically handled after executing the prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) code.