0

This has stumped me and Ive tried a few ways of doing of tutorials and on stack answers but its still not building.

So basically:

  • Im getting core data and then placing that into an array. (works fine)
  • After that Im just display first and last name in the cells (works fine)
  • User taps on cell to see athleteDetalView

I have placed a few print statements to see where its going wrong.

Thanks In advance for your input.

import UIKit
import CoreData

class athleteViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

//labels
@IBOutlet weak var athleteLabel: UILabel!

//buttons
@IBOutlet weak var athleteCreate: UIBarButtonItem!

@IBOutlet weak var athleteTableView: UITableView!

var athleteArray:[Athlete] = []
var myIndex = 0


override func viewDidLoad() {
    super.viewDidLoad()

    athleteTableView.delegate = self
    athleteTableView.dataSource = self

    self.fetchData()
    self.athleteTableView.reloadData()

}


func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return athleteArray.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "athleteName", for: indexPath)
    let name = athleteArray[indexPath.row]
    cell.textLabel!.text = name.firstName! + " " + name.lastName!

    let athleteName = name.firstName!
    let lastName = name.lastName!
    let age = name.age!
    let sport = name.sport!
    let email = name.email!

    print(athleteName)
    print(lastName)
    print(age)
    print(sport)
    print(email)

    return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    myIndex = indexPath.row
    let currentCell



    //let storyboard = UIStoryboard(name: "Main", bundle: nil)
    //let destination = storyboard.instantiateViewController(withIdentifier: "athleteDetailsViewController") as! athleteDetailsViewController

    //let athName = athleteArray[myIndex]

    //testdata


    //test data


    performSegue(withIdentifier: "athleteDetailsSegue", sender: self)



    //self.navigationController?.pushViewController(destination, animated: true)


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if (segue.identifier == "athleteDetailsSegue") {
        var destination = segue.destination as! athleteDetailsViewController

        destination.firstNameString = athleteName
        destination.lastNameString = lastName
        destination.ageString = age
        destination.sportString = sport
        destination.emailString = email
        //destination.getImage = name.image
    }



}



func fetchData(){

    let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

    do{
        athleteArray = try context.fetch(Athlete.fetchRequest())
    }
    catch{
        print(error)
    }
}
Aaron Ross
  • 49
  • 1
  • 1
  • 7
  • What is your actual issue? You say this code doesn't build, but then you say that you put print statements for debugging, so do you get a compile or runtime error or no error at all? – Dávid Pásztor Aug 08 '17 at 14:13
  • Mainly used the print statements to see if the data was actually being loaded into the variables which it is. However its not sending them to the other view. Ive tried doing: let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell then the variables i want to pass as well following what the discuss here [link](https://stackoverflow.com/questions/28430663/send-data-from-tableview-to-detailview-swift) – Aaron Ross Aug 08 '17 at 22:41

1 Answers1

0

The variables that you are setting in tableView:cellForRowAt:indexPath: are local variables and are not available in the function prepareForSegue:. Try to declaring the variables at the top as properties of the class.

Jon Rose
  • 8,373
  • 1
  • 30
  • 36