Swift 3 从 UICollectionView 单元格继续

Swift 3 Segue from UICollectionView Cell

嗨,我是初学者,一直停留在一个非常基本的 segue 上!我只想找出用于发件人的正确语法,例如 sandals[indexPath.row]sandalName 谢谢

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
    }
}

如果你只想通过 segue 传递值,你需要简单地使用 subscript 访问数组,就像你在 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"]!)
    }
}