Home > database >  How to send data from collectionview to UiCollectionReusableView in swift?
How to send data from collectionview to UiCollectionReusableView in swift?

Time:03-18

Try to send "data" inside collectionview to uireusableview(footerview).

i make an init in footerview to get data that send from collectionview. But how to send it to footerview?

if kind == UICollectionView.elementKindSectionFooter {
          

            guard let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "TenorFooterView", for: indexPath) as? TenorFooterView else {
                fatalError()
            }
            let item = viewModel.itemsForRowAt(section: indexPath.section)

           //Want to send item.data to data in footerview here

            return view
        }

FOOTER VIEW

class TenorFooterView: UICollectionReusableView {
    @IBOutlet weak var buttonSK : Button!
    
    weak var delegate : tenorFooterDelegate!
    var data : String?
    
    init(data: String!) {
        self.data = data
        super.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height))
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        buttonSK.addTarget(self, action: #selector(onTapLaunch(_:)), for: .touchUpInside)
        
        // Initialization code
    }
    
    @objc
    func onTapLaunch(_ sender: UIButton){
        self.delegate.tapSK()
    }
    
}

CodePudding user response:

Actually solution is very simple

you can just assign value like that

if kind == UICollectionView.elementKindSectionFooter {
          

            guard let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "TenorFooterView", for: indexPath) as? TenorFooterView else {
                fatalError()
            }
            let item = viewModel.itemsForRowAt(section: indexPath.section)
            // here
            view.data = item.data
            view.delegate = self
            return view
        }

or you can make a configure function in TenorFooterView

func configure(data: String, delegate: tenorFooterDelegate) {
    self.data = data
    self.delegate = delegate
    // Do some stuff
}

and call like that

if kind == UICollectionView.elementKindSectionFooter {
          

            guard let view = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "TenorFooterView", for: indexPath) as? TenorFooterView else {
                fatalError()
            }
            let item = viewModel.itemsForRowAt(section: indexPath.section)
            // here
            view.configure(data: item.data, delegate: self)
            return view
        }
  • Related