【问题标题】:How to Send Collection View Cell Text via Segue如何通过 Segue 发送集合视图单元格文本
【发布时间】:2023-03-30 21:17:01
【问题描述】:

我正在尝试将集合视图单元格中包含的标签发送到另一个带有 segue 的视图控制器。

我的计划是,当用户点击集合视图单元格时,应用程序会转到下一个视图控制器,其中导航栏的标题会显示所选集合视图单元格中标签的文本。

我试过这个:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CCCollectionViewCell

    //itemSelected = items[indexPath.row] as String

    itemSelected = cell.pLabel.text!

    print(itemSelected)
}

在 prepareForSegue 中我没有编写任何代码,因为我不确定它是如何工作的。

我将块 '..items[indexPath.row] as String' 注释掉,因为它不会显示标签并添加了打印功能以查看将输出的内容,但它只输出情节提要中给出的名称。

我对 Xcode 很陌生,所以不熟悉 didSelect 和 prepareForSegue。我要做的就是将集合视图单元格中的文本发送到另一个带有 segue 的视图控制器。

【问题讨论】:

    标签: ios swift xcode uicollectionview segue


    【解决方案1】:
      func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    
            self.performSegue(withIdentifier: "contentVideoSegue", sender: indexPath)
    
    }
    
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
        if segue.identifier == "contentVideoSegue"{
            let selectedIndexPath = sender as? NSIndexPath
            let videoContentVC = segue.destination as! VideoContentController
            videoContentVC.text = items[selectedIndexPath.row] as String
        }
    }
    

    希望能帮到你:)

    【讨论】:

    • 请添加一些描述来解释答案
    • 首先在didSelectItemAt indexPath方法中调用sugue。在此调用覆盖 func prepare(for segue) 方法开始工作后。 @jjj
    【解决方案2】:

    从您的代码中,您没有调用 performSegue(withIdentifier:sender:),因此您可能已经创建了从 CollectionViewCell 到 DestinationViewController 的 segue。所以在prepareForSegue方法中使用这个单元格获取indexPath。

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    
         if let cell = sender as? UICollectionViewCell, 
            let indexPath = self.collectionView.indexPath(for: cell) {
    
             let vc = segue.destination as! SecondViewController //Cast with your DestinationController
             //Now simply set the title property of vc
             vc.title = items[indexPath.row] as String
         }
    }
    

    【讨论】:

      【解决方案3】:

      所以您不需要在 didSelect 中设置单元格,因为您已经在 cellForItemAtIndexPath 中进行了设置。

      您可能希望在 didSelectItemAtIndexPath 中调用 performSegue(withIdentifier: "SegueName", sender: indexPath)。然后在您的 prepareForSegue 中:

      override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
      
          guard let indexPath = sender as? IndexPath else { return }
      
          let collectionCell = collectionView.cellForItem(at: indexPath)
          let textToPass = collectionCell.textLabel.text
      
          let detailVC = segue.destination as? DetailViewController
          detailVC.passedInString = textToPass
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-11
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        相关资源
        最近更新 更多