UICollectionView为每个UICollectionViewCell提供不同的segue

时间:2015-05-21 16:00:19

标签: ios objective-c iphone

我有一个动态加载单元格的UICollectionView,但是当我按下单元格时,我想打开一个不同的UIViewController。 例如: -cell1打开UIViewController1 -cell2打开UIViewController2 -cell3打开UIViewController3

有人知道,是否可以做到这一点?

3 个答案:

答案 0 :(得分:3)

您需要从视图控制器创建一个segues,它将集合视图(不是单元格)保存到目标视图控制器,例如:

UICollectionViewController -> UIViewController1
UICollectionViewController -> UIViewController2
//...etc

请记住为每个segues添加标识符。

在代码覆盖UICollectionViewDelegate方法

collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)

在该方法中检查哪个单元被轻击(indexPath),如果是cell1,则使用正确的标识符调用segue:

performSegueWithIdentifier("goToVC1Identifier", sender: nil)

答案 1 :(得分:2)

UICollectionViewDelegate方法didSelectItemAtIndexPath

中以编程方式执行此操作
UIViewController *c = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:@"%li",(long)indexPath.row]];

[self.navigationController pushViewController:c animated:YES];
//or however you a moving to next viewcontroller 

确保您的viewcontrollers具有正确命名的标识符以匹配单元格

答案 2 :(得分:1)

您需要在collectionview代表的- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath中手动执行segue。