2017-05-29 10 views
1

カスタムヘッダーとセルを持つカスタムUICollectionViewControllerがあります。ヘッダーから親ビューにデータを取得する

私は、このようにして、collectionViewが応答するプロトコルを実装しました。このように、ヘッダークラ​​スのオブジェクトがアクセスされたときに、collectionViewで知っています。私はcollectionViewで、ボタンをタップすると、私はcollectionView

にそれが所有していることをいくつかのデータを取得するために、ヘッダが必要であることを私はそれをやりたい

は、私がcollectionViewに保存ボタンをタップすると言うことができます保存を実行するには、ヘッダから現在のデータが必要です。

どうすればこの問題を解決できますか?

はここで、現在のコードの一部です:

import UIKit 

// collection view class 
class EditUserProfileHeader: UICollectionViewCell, UITextViewDelegate { 
    var delegate: CustomHeaderDelegate? 

    //(... some default code ...) 
    override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
     let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: headerId, for: indexPath) as! CustomHeader 
     header.delegate = self 
     return header 
    } 

    // header delegate conform 
    func didTapPhoto() { 
     // this fires everytime the button is pressed inside the header 
     print("didTapPhoto was tapped inside the header") 
    } 


    func didTapSave() { 
     //here I need the header's data from it's labels and so on 
    } 

} 


protocol CustomHeaderDelegate { 
    func didTapPhoto() 
} 



class CustomHeader: UICollectionViewCell { 
    var delegate: CustomHeaderDelegate? 
    //(... some class regular code ...) 

    func didTapPhoto() { 
     delegate?.didTapPhoto() // here I pass to the collectionView that this button was pressed in the header 
    } 

} 

あなたが見ることができるように、私は、私は私がcollectionViewに知っているので、デリゲート関数を呼び出すdidTapButtonがヘッダーで発生するたびに、デリゲートを実装しましたことボタンが鳴った。

今私は反対にする必要があります。私がcollectionViewの中のボタンをタップすると、それを知るためにヘッダが必要であり、それが所望のデータを取得する必要があります。

誰も私にこの手を差し伸べることはできますか?

逆のことをする別のプロトコルを実装しようとしましたが、うまくいきませんでした。なぜなら、ヘッダークラ​​スで親のデリゲートがその特定のデリゲートに準拠していると言うことができず、 Iヘッダーオブジェクトをインスタンス化し、そのdelegate to self

おかげで設定した場合、私は

+0

解決済みのように質問してください/解消しました。 –

答えて

0

IはdidTapSaveに次の行を挿入し、所望のヘッダにアクセスすることを管理collectionViewで行っている()関数

let header = collectionView?.supplementaryView(forElementKind: UICollectionElementKindSectionHeader, at: IndexPath(item: 0, section: 0)) as? CustomHeader 

私はヘッダーの更新データにアクセスできます。

関連する問題