swift 如何将数据从 collectionview 发送到 UiCollectionReusableView?

How to send data from collectionview to UiCollectionReusableView in swift?

尝试将 collectionview 中的“数据”发送到 uireusableview(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
        }

页脚视图

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()
    }
    
}

其实解决方法很简单

你可以像那样赋值

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
        }

或者您可以在 TenorFooterView

中创建一个 configure 函数
func configure(data: String, delegate: tenorFooterDelegate) {
    self.data = data
    self.delegate = delegate
    // Do some stuff
}

然后这样打电话

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
        }