我有两个 UICollectionView
@IBOutlet weak var collectionView1: UICollectionView!
@IBOutlet weak var collectionview2: UICollectionView!
我正在分别使用函数获取每个 Collection View 的 indexPath。
func getIndexPathForSelectedCell() -> NSIndexPath?
{
var indexPath:NSIndexPath?
if collectionview1.indexPathsForSelectedItems()!.count > 0 {
indexPath = collectionview1.indexPathsForSelectedItems()![0]
}
return indexPath
}
func getIndexPathForSelectedCell2() -> NSIndexPath?
{
var indexPath2:NSIndexPath?
if collectionView2.indexPathsForSelectedItems()!.count > 0 {
indexPath2 = collectionView2.indexPathsForSelectedItems()![0]
}
return indexPath2
}
我正在为单元格触摸执行 segue,如下所示。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if let indexPath = getIndexPathForSelectedCell()
{
let DealsdetailViewController = segue.destinationViewController as! DealsDetailViewController
DealsdetailViewController.Dealsdata = Dealsdata[indexPath.row]
}
else if let indexPath2 = getIndexPathForSelectedCell2()
{
let ContainerviewController = segue.destinationViewController as! ContainerViewController
ContainerviewController.BTdata = BTdata[indexPath2.row]
}
}
如果我在第一个 Collection View 中单击一个单元格,那么当我在第二个 Collection View 中单击一个单元格时,segue 会正确执行
我有错误 在
let DealsdetailViewController = segue.destinationViewController as! DealsDetailViewController
这是第一个 if 语句条件值,我被困在这里
请帮助我,如何处理对每个 Collection View 的单元格单击执行两个 segue。
最佳答案
使用UICollectionView协议(protocol)中的方法
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = cellForItemAtIndexPath(indexPath)!
if collectionView == self.collectionView1 {
self.performSegueWithIdentifier("segue1", sender: cell)
} else if collectionView == self.collectionView2 {
self.performSegueWithIdentifier("segue2", sender: cell)
}
}
func prepareForSegue(segue: UIStoryBoardSegue, sender: AnyObject?) {
if segue.identifer == "segue1" {
let detailVC:DetailViewController = segue.destinationViewController as DetailViewController
// Your sender is cell. You have indexPath of them and can get his identity in dataSource.
//detailVC.name = ...
//detailVC.surname = ...
} else if segue.identifier == "segue2" {
//...
}
}
关于swift - 如何使用 push segues 处理两个 Collection View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36813103/