来自UICollectionView Cell的Swift 3 Segue

时间:2017-03-28 11:25:32

标签: ios swift segue uicollectionviewcell uistoryboardsegue

嗨,我是初学者,坚持一个非常基本的赛道!我只是想找出用于发送者的正确语法,例如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
    }
}

1 个答案:

答案 0 :(得分:2)

如果您只想使用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"]!)
    }
}