swift - 使用执行segue进入 Collection View

标签 swift uicollectionview

我需要将数据传递到带有索引路径的 Collection View 中。 我正在尝试使用这段代码,但我不知道为什么我有两倍的结果,一次可以,第二次为零。

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    self.performSegue(withIdentifier: "showDetails", sender: indexPath)


}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    if let cell = sender as? UICollectionViewCell,
        let indexPath = self.collectionView.indexPath(for: cell) {

        let secondViewController = segue.destination as! CollectionDetailViewController //Cast with your DestinationController
        //Now simply set the title property of vc
        secondViewController.people = person[indexPath.row]
    }
}

最佳答案

您收到 2 次对 prepare(for:sender:) 的调用,因为您的 Segue 是从 UICollectionViewCell 连接的。这将为您调用 prepare(for:sender:),因此您不需要此代码:

override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    self.performSegue(withIdentifier: "showDetails", sender: indexPath)


}

删除它,你就可以开始了。

关于swift - 使用执行segue进入 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54897288/

相关文章:

ios - 应用平移变换时 UICollectionViewFlowLayout 边缘单元格消失

ios - 如何在将 UIViewController 呈现给自定义时更改默认转换

ios - 是否可以使用 SpriteKit 创建复杂的平台游戏?

swift - RxSwift 使用附加时间标志限制 Debounce 触发周期性事件

ios - 自定义 UICollectionViewCell 自动布局 NSInternalInconsistencyException 错误

swift - UICollectionView 单元格在向上滚动或重新加载功能时随机消失 - iOS 9/Swift

ios - UICollectionView全宽高问题

ios - 基于变量的索引路径中项目的 UICollectionView 单元格

swift - 使用 CGAffineTransform 合并两个图像

python - 如何在 IOS 应用程序上本地解析 AIML 文件?