UICollectionView 在 prepareForSegue 中获取 indexPath
UICollectionView Getting indexPath in prepareForSegue
对于我的集合视图,我使用 didSelectItemAt
调用 performSegue
。我的问题是发件人是我的 vc 而不是单元格本身。所以在 prepareForSegue 中,我收到一个错误,因为我无法将 vc 转换为我的单元格类型。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "collectionSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? ImageViewController {
let cell = sender as! ImageCollectionViewCell
let indexPath = imageCollectionView.indexPath(for: cell)
destinationVC.buttonTag = indexPath?.row
}
}
您可以将发件人设置为任何您想要的。这可能有帮助:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView!.cellForItemAtIndexPath(indexPath) as! ImageCollectionViewCell
performSegue(withIdentifier: "collectionSegue", sender: cell)
}
试试这个
var currentIndex = 0
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.currentIndex = indexPath.row
performSegue(withIdentifier: "collectionSegue", sender: self)
//let cell = sender as! ImageCollectionViewCell
//performSegue(withIdentifier: "collectionSegue", sender: cell)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "collectionSegue" {
if let destinationVC = segue.destination as? ImageViewController {
//let indexPath = imageCollectionView.indexPath(for: cell)
//let indexPath = imageCollectionView.indexPath(for: sender as! ImageCollectionViewCell)
destinationVC.buttonTag = self.currentIndex
}
}
}
对于我的集合视图,我使用 didSelectItemAt
调用 performSegue
。我的问题是发件人是我的 vc 而不是单元格本身。所以在 prepareForSegue 中,我收到一个错误,因为我无法将 vc 转换为我的单元格类型。
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
performSegue(withIdentifier: "collectionSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? ImageViewController {
let cell = sender as! ImageCollectionViewCell
let indexPath = imageCollectionView.indexPath(for: cell)
destinationVC.buttonTag = indexPath?.row
}
}
您可以将发件人设置为任何您想要的。这可能有帮助:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView!.cellForItemAtIndexPath(indexPath) as! ImageCollectionViewCell
performSegue(withIdentifier: "collectionSegue", sender: cell)
}
试试这个
var currentIndex = 0
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.currentIndex = indexPath.row
performSegue(withIdentifier: "collectionSegue", sender: self)
//let cell = sender as! ImageCollectionViewCell
//performSegue(withIdentifier: "collectionSegue", sender: cell)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "collectionSegue" {
if let destinationVC = segue.destination as? ImageViewController {
//let indexPath = imageCollectionView.indexPath(for: cell)
//let indexPath = imageCollectionView.indexPath(for: sender as! ImageCollectionViewCell)
destinationVC.buttonTag = self.currentIndex
}
}
}