0

I am writing a Xcode program in Swift. I have a tableview controller with some labels and an image per cell. They have their data from a first view controller. So far so good. Now i would like the user to tap a cell which opens a new controller which contains the same label and image data as the cell. With the following code the new controller opens, nonetheless I don't no how to transfer the data. If someone could help me I would be so so grateful.

Here are the codes that i tend to use:

 override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {


    let cellIdentifier = "TableView"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! TableView


    let picture = pic[indexPath.row]



    cell.label1.text = picture.name1
    cell.photoImage.image = picture.photo
    cell.label2.text = picture.name2


    return cell


}

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


    tableView.deselectRowAtIndexPath(indexPath, animated: true)


    let cell = indexPath.row

    self.performSegueWithIdentifier("segue", sender: cell)

}

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

    if segue.identifier == "segue"{

        var row = tableView.indexPathForSelectedRow

        let viewController = segue.destinationViewController as! vc2

    }

}

PS:I have a segue from the image to the new controller with the identifier "segue". PPS: Of course i have tried the following method: Send data from TableView to DetailView Swift but when i run my program I get an error with the information that my labels were unexpectedly found nil

Community
  • 1
  • 1
j3a3n3
  • 1
  • 2

1 Answers1

0

You should not need to override didSelectRowAtIndexPath or call performSegueWithIdentifier to do this. Connect your segue in the IB file dragging from a table view controller's cell to the second controller. You should then pass the data to the controller in prepareForSegue, in the segue.destinationViewController. You set the public properties on that destination controller.

Also make sure your labels in your prototype cell have been connected in IB. If they are they, should not be nil. Set a breakpoint on the cellForRowAtIndexPath to verify this.

Unless you are talking about the labels in your destination controller. These also need to be hooked up in IB. You would then set them in prepareForSegue. You would get the appropriate data from the

let viewController = segue.destinationViewController as! vc2
var path = tableView.indexPathForSelectedRow

viewController.destLabel.text = arrayData[path.row].myData // or whatever data you have from your model.
possen
  • 8,596
  • 2
  • 39
  • 48
  • i did it the way you suggested. the data was still not transferred. Furthermore i get now an error (label found nil) when trying to create a additional cell :( – j3a3n3 Sep 26 '16 at 07:38