2016-06-18 8 views
0

indexPathに対して2つの異なるセルを持つことができるように、collectionViewからindexPathを自分のセルに渡そうとしています。私はコントローラのインスタンスを渡して、セル内のセルに設定しようとしました。プロトコルデリゲートも試しましたが、どちらもうまくいかないようです。私は非常に頻繁にデリゲートを使用するので、私はそれを正しくやっていることを知っています。この場合、私のデリゲート関数はセルでデリゲートを自分自身に設定しても呼び出されません。私は何が起こっているか分からないが、何も動作していないようだ。indexPathに2つの異なるセルを指定してcollectionViewからindexPathを渡します。

CollectionView VC

protocol ActivityAboutVCDelegtae: class { 

    func passIndexPath(indexPath:Int) 
} 

class ActivityAboutVC: UICollectionViewController, UICollectionViewDelegateFlowLayout { 

    var delegate:ActivityAboutVCDelegtae? 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     self.collectionView!.registerClass(ActivityAboutCell.self, forCellWithReuseIdentifier: CELL_ID) 
     self.collectionView?.pagingEnabled = true 
     self.collectionView?.alwaysBounceVertical = false 
    } 

    // MARK: UICollectionViewDataSource 
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 

     return 2 
    } 

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CELL_ID, forIndexPath: indexPath) as! ActivityAboutCell 

     if delegate != nil { 
      delegate?.passIndexPath(indexPath.row) 
     } 

     return cell 
    } 

CollectionViewCell

import UIKit 

class ActivityAboutCell: BaseCell, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout, ActivityAboutVCDelegtae { 

    lazy var actVc:ActivityAboutVC = { 
     let vc = ActivityAboutVC() 
      vc.delegate = self 
     return vc 
    }() 


    func passIndexPath(indexPath: Int) { 
     print(indexPath) 
    } 

    override func setupView() { 
     super.setupView() 

     ActivityAboutSetup() 
     backgroundColor = .whiteColor() 
    } 
+0

「actVc」は**ストーリーボードのコントローラではありません**。それはまったく異なるインスタンスです。そのコントローラへの実際の参照が必要です – vadian

答えて

0

1)あなたが達成したいのか明確ではないが、以下のソリューションLESTであなたを呼び出す方法を提供しますあなたはpassIndexPathメソッドです。 UICollectionViewアイテムではなく、行を使用)

cell.passIndexPath(indexPath.row) 

2と

if delegate != nil { 
    delegate?.passIndexPath(indexPath.row) 
} 

を交換 。最終的な結果は

cell.passIndexPath(indexPath.item) 

3)actVcプロパティが不要な、あるいは間違っているに見えるべきである理由です。

+0

私はactVCがまったく役に立たなかったことを知っていましたが、私はデリゲートを設定しようとしました。あなたが示したことはうまくいった。ありがとうございました!! –

関連する問題