0

Hi I'm a beginner and stuck on a very basic segue! I just want to find out the correct syntax to use for the sender e.g sandals[indexPath.row]sandalName Thanks

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! shoeCollectionViewCell
    let sandalCell = sandals[indexPath.row]
    cell.sandalImage.image = UIImage(named: sandalCell["image"]!)
    cell.sandalStyleName.text = sandalCell["styleName"]
    cell.sandalPrice.text = sandalCell["price"]
    return cell
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "shoeDetailSegue"{
        var detailPage = segue.destination as! shoeDetailViewController
        let selectedCell = sender as! UICollectionViewCell
        let indexPath = collectionView?.indexPath(for: cell)
        detailPage.getName = sandals[indexPath!.row].sandalName
        detailPage.getPrice = sandals[indexPath!.row].sandalPrice
        detailPage.getImage = sandals[indexPath!.row].sandalImage
    }
}
Jeff
  • 3,829
  • 1
  • 31
  • 49
Zed
  • 25
  • 4
  • http://stackoverflow.com/a/41887007/5461400 try this – Harshal Valanda Mar 28 '17 at 11:26
  • Show us the declaration of `sandals` array – Nirav D Mar 28 '17 at 11:30
  • as a note: `var detailPage = segue.destination as! shoeDetailViewController` should be `var detailPage = segue.destination as! ShoeDetailViewController` ignoring the bad variable name conventions. – TheCodingArt Mar 28 '17 at 12:00
  • The values from your array of dictionaries may also need to be type cast. I'm not sure how you're going from: `sandalCell["styleName"]` to `sandals[indexPath!.row].sandalName`. One is using [subscript](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Subscripts.html) access, the other is using [properties](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html). Those are 2 completely different things. – TheCodingArt Mar 28 '17 at 12:02
  • Its a simple dictionary. I've got the collectionView to work and fill the cells correctly – Zed Mar 28 '17 at 12:14
  • var sandals = [["styleName":"Heeled Mule Sandal", "image":"gold mule sandal", "price":"£595"],["styleName":"Heeled Sequinned Mule Sandal", "image":"sequinned mule sandal", "price":"£649"],["styleName":"Deco Sandal", "image":"art deco sandal", "price":"£395"],["styleName":"Frayed Sandal", "image":"frayed sandal", "price":"£395"],["styleName":"Satin Flatform", "image":"flatform sandal", "price":"£295"],["styleName":"Striped Espadrille", "image":"striped espadrille", "price":"£395"]] – Zed Mar 28 '17 at 12:15
  • @Zed It means you are having array of dictionary then why you want to access it with property instead of subscript like you have done in `cellForItemAt`. – Nirav D Mar 28 '17 at 12:28
  • @Zed if you want to access your array with property then you need to make array of custom class objects. – Nirav D Mar 28 '17 at 12:31
  • I want the information in the cell to segue to a UIImage and text fields on the next ViewController (ShoeDetailViewController) I'm a novice and trying to piece things together from various different sources! – Zed Mar 28 '17 at 12:35

1 Answers1

2

If you want to just pass the value with segue you need to simply access the array with subscript same way you are doing in cellForItemAt.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "shoeDetailSegue"{

        var detailPage = segue.destination as! shoeDetailViewController
        let selectedCell = sender as! UICollectionViewCell
        let indexPath = collectionView?.indexPath(for: cell)
        let sandal = sandals[indexPath!.row]
        detailPage.getName = sandal["styleName"]!
        detailPage.getPrice = sandal["price"]!
        detailPage.getImage = UIImage(named: sandal["image"]!)
    }
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183