James Wood
James Wood

Reputation: 1

How to send information from custom cells to a view controller?

Im currently trying to send some information from custom cells to the next view controller in Xcode 7 with Swift 2.

I can send information fine if it is from a label etc but i cannot do it from cells as I'm unable to get the specific cell the user tapped. I have tried to take information from tutortials on how to get the index path but always get an error.

Layout of View Controllers

Code for main VC and error

Any ideas on how to fix this?

Upvotes: 0

Views: 290

Answers (1)

jignesh Vadadoriya
jignesh Vadadoriya

Reputation: 3310

  1. You need to create segue like ViewController to ExpanView Make sure not for UITableViewCell To ExpanView
  2. your tableView.allowsSelection = true And set tableView.delegate = self
  3. Implyment didSelectRowAtIndexPath Method of tableView like this way.

    func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
         self.performSegueWithIdentifier("exapanView", sender: indexPath)
    }
    
  4. implement prepareForSegue method like this way

    func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "exapanView" {
        let selectedIndexPath = sender as! NSIndexPath
        let nameString = self.name.objectAtIndex(selectedIndexPath.row)
    }
    }
    

Upvotes: 2

Related Questions