我需要将数据传递到带有索引路径的 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/