【问题标题】:Swift 3 Segue from UICollectionView Cell来自 UICollectionView 单元的 Swift 3 Segue
【发布时间】:2017-08-21 10:52:18
【问题描述】:

您好,我是一个初学者,并且停留在一个非常基本的 segue 上!我只是想找出用于发件人的正确语法,例如凉鞋[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
    }
}

【问题讨论】:

  • 向我们展示sandals数组的声明
  • 注意:var detailPage = segue.destination as! shoeDetailViewController 应该是 var detailPage = segue.destination as! ShoeDetailViewController 忽略错误的变量名称约定。
  • 您的字典数组中的值也可能需要进行类型转换。我不知道你是怎么从:sandalCell["styleName"]sandals[indexPath!.row].sandalName。一种是使用subscript 访问,另一种是使用properties。这是两个完全不同的东西。
  • 它是一个简单的字典。我已经让 collectionView 正常工作并正确填充单元格

标签: ios swift segue uicollectionviewcell uistoryboardsegue


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多