如何在UICollectionVIew中对不同的控制器执行segue

时间:2017-04-10 16:52:04

标签: swift3

enter image description here我想根据UICollectionView中的选择来转换到不同的控制器:

覆盖func collectionView(_ collectionView:UICollectionView,didSelectItemAt indexPath:IndexPath){

    if indexPath.item == 0 && indexPath.section == 0 {

    print ("Item 0 and section 0 selected")

        Perform Segue to Controller1 


    } else if (indexPath.item == 1 && indexPath.section == 0 ){

        ----> Perform Segue to Controller 2 


        print ("Item 1 and section 0 selected")
    } else {
        print("Not selected ")
    }



}

根据Collection View中的选择,它应该转换为不同的UiCollectionView控制器

Tried this command

self.performSegue(withIdentifier:“Selection1”,sender:self)

但它错了:  'NSInternalInconsistencyException',原因:'无法使类型的视图出列:具有标识符ProductCell的UICollectionElementKindCell - 必须为标识符注册一个nib或类或在故事板中连接原型单元'

执行此类Segue的最佳方式是什么? 请指教

由于

1 个答案:

答案 0 :(得分:0)

覆盖func collectionView(_ collectionView:UICollectionView,didSelectItemAt indexPath:IndexPath){

    if indexPath.item == 0 && indexPath.section == 0 {




        self.performSegue(withIdentifier: "Selection1", sender: self)







    } else if (indexPath.item == 1 && indexPath.section == 0 ){


     self.performSegue(withIdentifier: "Selection2", sender: self)

        print ("Item 1 and section 0 selected")
    } else {
        print("Not selected ")
    }



}

另外,为了摆脱错误,我必须更新集合可重用视图中的标识符enter image description here