2017-07-11 3 views
0

didSelectItemAtIndexPath funcをUICollectionViewCellに書き込んでUICollectionViewControllerを選択しました。私は2つの方法でコードを書いたが、それは全く機能しない。また、私はそこに間違いがない。didSelectはUICollectionViewControllerを選択するためにUICollectionViewCellで機能しません

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

    if indexPath.item == 0 { 

     let layout = UICollectionViewFlowLayout() 
     let controller1 = DigitalSLRCon(collectionViewLayout: layout) 
     let nav = UINavigationController() 
     nav.pushViewController(controller1, animated: true) 

// OR

 let layout = UICollectionViewFlowLayout() 
     let controller1 = DigitalSLRCon(collectionViewLayout: layout) 
     navigationController?.pushViewController(controller1, animated: true) 

    } 
+0

のように細胞内のデフォルト変数をオーバーライドすることができ、私はので、私は今何ができるprotocal –

答えて

1

didSelectItemAtIndexpathUICollectionViewDelegateの関数である - あなたは、細胞内で、しかし、あなたのCollectionViewのデリゲートにコレクションビューが含まれているので、おそらくビューコントローラを、それを実装していません。コレクションビューのdelegateプロパティにコントローラを表示

  1. コレクションビューを保持しているのUIViewControllerがUICollectionViewDelegateプロトコル
  2. 割り当てに準拠して作成します
  3. ビューコントローラ
+0

としてUICollectionviewCellでUICollectionViewDelegateとUICollectionViewDatasourceを追加しました –

+0

@AzmalTech編集済みの回答を参照 –

+0

別の方法を教えてください。 –

0

didSelectItemAtIndexpath機能を実装し削除してください代理人はUICollectionViewCellセルの代理人ではなく、UICollectionViewハンドラ

あなたがセルに

をいくつかの要素を変更したい場合は、この

override var isSelected: Bool { 
    didSet { 
     if isSelected { 
     //do something 
     } else { 
     //not selected 
     } 
    } 
} 

override var isHighlighted: Bool { 
    didSet { 
     if isHighlighted { 
     //do something 
     } else { 
     //not selected 
     } 
    } 
} 
関連する問題