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