0
votes

so I have been trying to use Core Data with a logbook app that records information. In this case it is flying hours. So I input data and it uses Core Data to save/store is as a set value but I have only made it display a small amount of the saved info in the table view(see in code below).

What I need help with, it making it so I can click on each tableViewCell which goes to a VC where it has all the info which the user inputted into the app(as it is a logbook)

How will it be possible so that the user will see their specific info for that specific cell which have different info stored as they are all different logs

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

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return logArray.count
}
//WHATS IN THE TABLE VIEW CELL FUNCTION/////
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) 
    let log = logArray[indexPath.row]
    cell.textLabel!.text = "Flight:" + "  " + log.date! + "      Click For More Info -->"
    return cell
}
1

1 Answers

0
votes

First create variable for log in your UITableViewCell

var log: Log?

now in cellForRowAt set log of cell independent on indexPath.row

cell.log = logArray[indexPath.row]

then create variable in second ViewController.

var selectedLog: Log?

Now in Table View delegate method didSelectRowAtperform segue with sender as that cell

 func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegueWithIdentifier("yourIdentifier", sender: tableView.cellForRow(at: indexPath))
}

And finally in prepare for segue method declare destination ViewController as YourSecondViewController and set its selectedLogProperty

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if segue.identifier == "yourIdentifier" {
         let destinationVC = segue.destination as! YourSecond ViewController
         let senderCell = sender as! YourTableViewCell
         destinationVC.selectedLog = senderCell.log!
    }
}