我有一个动态加载单元格的UICollectionView,但是当我按下单元格时,我想打开一个不同的UIViewController。 例如: -cell1打开UIViewController1 -cell2打开UIViewController2 -cell3打开UIViewController3
有人知道,是否可以做到这一点?
答案 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。