0

I am currently working on an app that puts API data into tableview then view the data in a detailsViewController.

My code looks about right to me, and I'm beginning to wonder maybe all the information should be in the tableview for me to pass the data to another controller. Is it possible to pass data to another controller without having to put all the information in the tableview cell?

EDIT: If I were to fetch data in my detailsViewController from the fetchedSpell array, how would I let the detailsViewController know which data to show based on the clicked cell?

I've already tried passing it through but nothing is showing in the details controller when i click on the cell.

Table View Select

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: 
  IndexPath) {

    let storyBoard: UIStoryboard = UIStoryboard(name:"Main", 
bundle: nil)
    let SpellDetailsViewController = 
storyBoard.instantiateViewController(withIdentifier: 
 "SpellDetailsViewController") as! SpellDetailsViewController
    let detail = fetchedSpell[indexPath.row]

    SpellDetailsViewController.spellText = detail.spell
    SpellDetailsViewController.effectText = detail.effects
    SpellDetailsViewController.typeText = detail.types

    self.present(SpellDetailsViewController, animated: true, 
completion: nil)
}

Spell Details Controller

class SpellDetailsViewController: UIViewController, 
UINavigationControllerDelegate {
@IBOutlet weak var spellLabel: UILabel!
@IBOutlet weak var typeLabel: UILabel!
@IBOutlet weak var effectLabel: UILabel!

var spellText: String!
var typeText: String!
var effectText : String!
override func viewDidLoad() {
    super.viewDidLoad()

    spellLabel.text = spellText
    typeLabel.text = typeText
    effectLabel.text = effectText
}

Tablieview and Details Page

Grace
  • 117
  • 1
  • 10
  • What do you mean by **without having to put all the information in the tableview cell**? – PGDev Jul 03 '19 at 06:22
  • 2
    Possible duplicate of [Passing Data between View Controllers](https://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Dris Jul 03 '19 at 06:37
  • @PGDev So my tableview cell only has the UILabel SpellName which contains data from my Json array. once I click on the cell it transfers me to detailviewcontroller that should show the SpellName and also other two datas like spell type and spell effects. However none of the data our passing through. I usually would put all the info on tableview cell and grab that and pass through details – Grace Jul 03 '19 at 06:38
  • But in the code you added, it doesn't seem like you're fetching the data from UITableViewCell. – PGDev Jul 03 '19 at 06:39
  • @PGDev yeah thats exactly what I was trying to do. So i was wondering if i had to put all the information on tableviewcell to send it to the detailviewcontroller. More like trying to fetch data from the array instead. if that makes sense – Grace Jul 03 '19 at 06:41
  • No you don't. You can simply fetch it from the `fetchedSpell` array itself. – PGDev Jul 03 '19 at 06:42
  • @PGDev how do i change my code? When the cell is clicked, how do i let the detail controller know that that's the spell I picked? – Grace Jul 03 '19 at 06:43

1 Answers1

1

According to the screenshot you are using a segue so you must not instantiate the controller.

  • (Re)connect the segue from the table view cell (instead of the controller) and set the identifier to "SpellDetailsViewController"
  • Delete the entire didSelectRowAt method
  • Implement prepare(for segue. When the user taps on a cell, prepare(for segue is called and the cell is passed as sender

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        guard segue.identifier == "SpellDetailsViewController",
            let cell = sender as? UITableViewCell,
            let indexPath = tableView.indexPath(for: cell) else { return }
    
        let detail = fetchedSpell[indexPath.row] 
        let spellDetailsViewController = segue.destination as! SpellDetailsViewController
        spellDetailsViewController.spellText = detail.spell
        spellDetailsViewController.effectText = detail.effects
        spellDetailsViewController.typeText = detail.types  
    }
    
  • And in the detail view controller declare the string properties as non-optional empty string for example

    var spellText = ""
    
vadian
  • 274,689
  • 30
  • 353
  • 361
  • the line let indexPath = tableView.indexPath(for: cell) else { return } shows as ambiguous how do i fix that? – Grace Jul 03 '19 at 07:11
  • The code assumes that there is an `IBOutlet weak var tableView : UITableView!` connected to the table view instance. – vadian Jul 03 '19 at 07:14
  • should i delete the push segue i attached to the detailsviewcontroller before? – Grace Jul 03 '19 at 07:19
  • Yes, delete it and connect a new one from the table view cell to the detail view controller. – vadian Jul 03 '19 at 07:20
  • Then most likely the identifier is wrong or the segue is not connected from the **cell**. Is `prepare(for segue` called at all? Set a breakpoint or insert a `print` line – vadian Jul 03 '19 at 07:41
  • I've got it. It wasn't any of those. It turns out i needed to restart xCode for the data to show haha thank you so much for your help. I learnt alot! – Grace Jul 03 '19 at 07:52