2

I have got a collection view inside a table view (Netflix App Style). I followed this tutorial: https://www.thorntech.com/2016/01/scrolling-in-two-directions-like-netflix-part-2-making-api-calls/

Now I want to implement a function that whenever I click on a collection view, it will pass the employee's name to the next view controller. Here is my code:

import UIKit

class CategoryRow: UITableViewCell, UICollectionViewDataSource {

    @IBOutlet weak var collectionView: UICollectionView!

    var jobpost:JobPosition?
    var employee: Employee?

    var myname = ""


    func collectionView(_ collectionView: UICollectionView,     numberOfItemsInSection section: Int) -> Int {
        return jobpost!.Employees.count
    }

    func collectionView(_ collectionView: UICollectionView,     cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "videoCell", for: indexPath) as! EmployeeCell
    cell.employeephoto.image = UIImage(named: "spanner")

    cell.employeename.text = jobpost?.Employees[indexPath.row].name

    return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let myname = jobpost?.Employees[indexPath.row].name
    //print(myname)
}

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

        let EmployeeDetailPage = segue.destination as? EmployeeDetailViewController

        EmployeeDetailPage?.employeename = myname
        print(myname)
}

Now my problem is, the whole PrepareForSegue function didn't actually run. The app runs normally without any error but the data just didn't pass through? (i.e. the print(myname) didn't actually print anything).

Is there something I missed out? Very much appreciated.

Xenos
  • 73
  • 2
  • 11
  • this already has an answer for it http://stackoverflow.com/questions/42739412/send-data-from-tableviewcontroller-to-detailviewcontroller/42744464#42744464 you do not need to use `prepareForSegue` method. you can achieve this only by using `didSelectRowAtIndexPath` methos. – caldera.sac May 22 '17 at 04:19
  • Possible duplicate of [Pass data through segue](https://stackoverflow.com/questions/26207846/pass-data-through-segue) – Lal Krishna Apr 12 '18 at 08:34

2 Answers2

3

Step 1

Create a global variable in your CategoryRow

var myname: String!

Step 2

Change the didSelectItemAt func like below

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    myname = jobpost?.Employees[indexPath.row].name

    performSegue(withIdentifier: "EmployeeDetailViewController", sender: myname)
}

Step 3

Create a variable in EmployeeDetailViewController

var employeename: String!

Step 4

Give a identifier to your segue. Here I give "EmployeeDetailViewController" Then change the prepareForSegue func like below

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

        if segue.identifier == "EmployeeDetailViewController" {

            if let EmployeeDetailVC = segue.destination as? EmployeeDetailViewController {

                EmployeeDetailVC.employeename = myname

            }

        }
}
malhal
  • 26,330
  • 7
  • 115
  • 133
Alwin
  • 1,476
  • 3
  • 19
  • 35
  • It's a hack to use the sender param to pass data. The sender must be the UIView or UIViewController responsible for the segue to work correctly. I've edited it to fix it to correctly use the myname property for the data. – malhal Jan 21 '19 at 14:12
1

It looks like your problem is that you are setting a local let constant called myname in your collectionView(_:didSelectItemAt:) method instead of setting your class's myname property.

let myname = jobpost?.Employees[indexPath.row].name

should be:

myname = jobpost?.Employees[indexPath.row].name
joltguy
  • 536
  • 5
  • 6