0

I want to pass image and other data between UITableViewController (It have custom UITableViewCell). Into the function prepareForSegue I made following, but it don't work.

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "identifierDetail" {
        if let index = self.tableView.indexPathForSelectedRow() {
            let controller = (segue.destinationViewController as? UINavigationController)?.topViewController as? DetailViewController

            let cellIndentifier: String = "NewsCell"

            var cell: ParseTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIndentifier) as? ParseTableViewCell

            controller?.image = cell?.imageViewCell.image
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alexander Khitev
  • 6,417
  • 13
  • 59
  • 115
  • 2
    Define "doesn't work'. FYI - those are the least helpful words a developer can state. You need to be clear about what should happen and what actually happens. Update your question with relevant details. – rmaddy Jun 07 '15 at 22:42

2 Answers2

2

You are calling dequeueReusableCellWithIdentifier this gives you a new cell, if you want to access the values in that cell you need to access the data source for the cell in question as in the code below:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "identifierDetail" {
        if let index = self.tableView.indexPathForSelectedRow() {
            let controller = (segue.destinationViewController as? UINavigationController)?.topViewController as? DetailViewController

            let selectedData = dataSource[selectedIndexPath.row] //here dataSource is here the data to populate your table come from

            controller?.image = cell?.imageViewCell.image
        }
    }
}
Icaro
  • 14,585
  • 6
  • 60
  • 75
0

This

var cell: ParseTableViewCell? = tableView.dequeueReusableCellWithIdentifier(cellIndentifier) as? ParseTableViewCell

is getting a 'new' cell (or it could be reusing an existing cell), not getting the selected cell from your tableview.

You shouldn't use cells as an alternative to your data model - they are simply a view of your data. Once you have retrieved the indexPath of the selected cell, simply index into your array or other data model to retrieve the image that you assigned to the cell in cellForRowAtIndexPath

Paulw11
  • 108,386
  • 14
  • 159
  • 186