我需要将我的数据传递到带有索引路径的集合视图中。 我正在尝试使用此代码,但我不知道为什么我得到的结果是结果的2倍,一次是可以的,第二次是nil。
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]
}
}
答案 0 :(得分:0)
您收到2个致电prepare(for:sender:)
的电话,因为您的Segue是从UICollectionViewCell
连线的。这将为您调用prepare(for:sender:)
,因此您不需要此代码:
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "showDetails", sender: indexPath)
}
删除它,您应该会很好。