2017-12-14 6 views
1

セル内に別のUICollectionView(プログラムで追加された2番目のレベル)を持つUICollectionViewCell(ページングが有効、幅は&の高さです)があります。 2番目のレベルのUICollectionViewには、xib(CustomMenuViewController)を使用してUICollectionViewCellがあります。 このxibファイルには、ボタンがあります。このボタンを押すと、別のUIViewControllerに行きたいと思っています。UICollectionViewCell xibからUIViewControllerへの切り替え

class Cellnew: UICollectionViewCell, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout{ 

    let cellId = "mycell"; 

    override init(frame: CGRect) { 
     super.init(frame: frame); 

     setupViews(); 
     self.collectionView.register(UINib(nibName: "subMenuCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "mycell") 
      } 

    lazy var menuBar : MenuBar = { 
     let mb = MenuBar() 
     return mb 

    }() 

    func setupViews(){ 
     addSubview(collectionView); 

     collectionView.delegate = self; 
     collectionView.dataSource = self; 

     collectionView.leftAnchor.constraint(equalTo: leftAnchor).isActive = true; 
     collectionView.rightAnchor.constraint(equalTo: rightAnchor).isActive = true; 
     collectionView.topAnchor.constraint(equalTo: topAnchor).isActive = true; 
     collectionView.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true; 
     collectionView.heightAnchor.constraint(equalToConstant: 200).isActive = true 


    } 

    let collectionView: UICollectionView = { 
     let layout = UICollectionViewFlowLayout(); 
     layout.minimumLineSpacing = 30 
     layout.minimumInteritemSpacing = 0 
     layout.scrollDirection = .vertical; //set scroll direction to horizontal 
     let cv = UICollectionView(frame: .zero, collectionViewLayout: layout); 
     cv.backgroundColor = UIColor.hexStringToUIColor(hex: "F9F9F9") 
     cv.translatesAutoresizingMaskIntoConstraints = false; 
     cv.contentInset = UIEdgeInsetsMake(50, 0, 150, 0) 

     return cv; 
    }(); 


    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

     let cell : subMenuCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! subMenuCollectionViewCell 

     cell.mImagePress.addTarget(self,action:#selector(self.toCustommenu), for: .touchUpInside) 
     return cell; 
     } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { 
     return UIEdgeInsetsMake(20, 10, 20, 10); 
       } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 5; 
       } 



    @objc func toCustommenu(){ 

     //What can i do here 


       } 




    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { 
     collectionView.reloadData() 
     print("menutapped") 
     let myDict: [String: Any] = ["CellNo": indexPath.row] 
     NotificationCenter.default.post(name: .refresh, object: myDict) 


    } 
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
     return CGSize(width:(self.frame.width/2)-10-10, height: self.frame.height/3); 
    } 
     required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 
} 
+0

あなたは、UIViewControllerから別のものにsegueを渡すことができます。親ViewController(あなたが望むように、Closureを使用してCollectionViewCollectionViewViewControllerを理解していれば委任してください)に、ある時点でSegueを実行する必要があります。 – Larme

+0

私はコードを教えてくれますか?私は新しいiosアプリケーションの開発私はuiviewcontrollerオブジェクトを持っていない。私はxibファイル(xibの私はそこに持っている) –

+0

あなたの最初のコレクションビュー表示 - ビューコントローラは、ストーリーボードにありますか? – MBN

答えて

0

1.Connect A parentViewControllerからdestinationViewControllerにsegueします。

2.Parentビューコントローラ

class parentViewcontroller : ViewController , ParentCellProtocol 
{ 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 

    { 
     Create instance of cell - parentCollectionViewCell 
     //Set the delegate 
     parentCollectionViewCell.parentDelegate = self 
    } 

    //Implement the ParentCellProtocol delegate method 
    func buttonClickedFromParentCell() 
    { 
     //Perform segue here 
    } 
} 

3.Parentカスタムセル

protocol ParentCellProtocol 
    { 
     func buttonClickedFromParentCell() 
    } 

    class ParentCell : ChildCellProtocol 
    { 
     //Create an instance of protocol. 
     var parentDelegate : ParentCellProtocol? 

     //Implement ChildCellProtocol Method 
     func buttonClickedFromChild(){ 

     parentDelegate.buttonClickedFromParentCell() 
     } 


     func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
     { 
     Create instance of cell - childCustomCell 
     //Set the delegate 
     childCustomCell. childDelegate = self 
     } 
    } 

4.Childカスタムセル

protocol ChildCellProtocol { 
    func buttonClickedFromChild() 
} 

class ChildCell 
{ 
    //Create an instance of protocol. 
    var childDelegate : ChildCellProtocol? 

    //call the delegate method on button click. 
    func toCustommenu(){ 

     //What can i do here 
     childDelegate.buttonClickedFromChild() 
    } 
} 

は、それはあなたのお役に立てば幸い!

+0

助けてくれてありがとう。 cellnewのcollectionviewは別のparentcollectionviewcellにロードされます。今何ができますか? –

+0

同じ手順を実行することもできます。 – MBN

+0

[viewcontroller [collectionview [collectionviewcell [collectionview [collectionviewcell [xib]]]]]これはビューの階層構造になっています。自己オブジェクト、私はviewcontrollerから望む –

0

私はあなたがストーリーボード内の最も外側のコレクションビューが含まれている初期UIViewControllerを持っていると仮定しています。私はまた、これが第2のUIViewControllerによって制御される別のビューに移行すべきビューであると仮定しています。

コレクションビュー内でプログラム的に作業しているので、初期ビューコントローラからセカンダリビューコントローラへのセグを作成し、それを名前でトリガすることが最も効果的です。

  1. ストーリーボード上の最初のビューコントローラとセカンダリビューコントローラを見つけます。これを行うには

  2. 初期表示コントローラーの上部バーの黄色の円からコントロールをドラッグして、2次ビューにドラッグします。
  3. ポップアップメニューの[Manual Segue]で、[Show]になる可能性が高い正しいオプションを選択します。
  4. 作成されたセグをクリックし、識別子を付けます。あなたのコードで
  5. 、あなたが移行を実行したいとき、UIViewControllerに(あなたがセグエに指定した識別子の文字列を渡す)performSegue(withIdentifier:sender:)を呼び出して、最も外側のコレクションビューが中に含まれている。
+0

ありがとうございます。私は初期のviewcontrollerを持っていません。トリガーされたボタンはストーリーボードの一部ではないxib上にあります。 –

+0

Ok ...ストーリーボードを最初に実装したのは、このような状況を緩和するためだと思います。迅速に使用している場合は、レガシーコードで作業していないと仮定しますので、それらを調べることをおすすめします。 – whistler

関連する問題