如何访问从 UICollectionView 到 UICollectionReusableView 的出口?

How to access outlets from UICollectionView to UICollectionReusableView?

已在 ViewController 中注册 header:

self.itemCollectionView.register(NewSubscriptionRequestCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView")

在 CollectionView 委托方法中:

extension UpcomingSubscriptionListViewController:  UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout {

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

    let headerView = itemCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView", for: indexPath) as! NewSubscriptionRequestCollectionReusableView
    headerView.newSubscriptionRequestViewDelegate = self
    return headerView
}

我的Headercollection文件

protocol NewSubscriptionRequestViewDelegate : class {

}

class NewSubscriptionRequestCollectionReusableView: UICollectionReusableView {

weak var newSubscriptionRequestViewDelegate:  NewSubscriptionRequestViewDelegate?
var contentView : NewSubscriptionRequestCollectionReusableView?

override init(frame: CGRect) {
    super.init(frame: frame)
    let contents = Bundle.main.loadNibNamed("NewSubscriptionRequestCollectionReusableView", owner: self, options: nil)?.first as! NewSubscriptionRequestCollectionReusableView
    contents.frame.size.width = UIScreen.main.bounds.width
    contents.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    self.addSubview(contents)
}

@IBOutlet var pdfStatus: UILabel!
@IBOutlet var nutritionView: UIView!
@IBOutlet var dateView: UIView!
@IBOutlet var nutritionPlanLabel: UILabel!
@IBOutlet var calendarLabel: UILabel!

override func awakeFromNib() {
    super.awakeFromNib()
    dateView.layer.cornerRadius = 10
    nutritionView.layer.cornerRadius = 10
}


required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}
}

我想将pdfStatus标签从viewController更改为class但是当我试图通过下面的代码实现时。它给了我 nil

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {

   let headerView = itemCollectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView", for: indexPath) as! NewSubscriptionRequestCollectionReusableView
    headerView.newSubscriptionRequestViewDelegate = self
    headerView.pdfStatus.text = "something" .  // Getting nil here
    return headerView
}

您在注册 reusableView 时实施了错误的方法。您需要使用此方法注册您的 reusableView。

collectionView.register(UINib, forSupplementaryViewOfKind:, withReuseIdentifier:)

没有

collectionView.register(Class, forSupplementaryViewOfKind:, withReuseIdentifier:)

如果你没有注册你的 XIB,UICollectionView 没有任何关于 IBOutlet 的数据(还有 IBAction)。因此,您的单元格可以使用定义的 init 方法创建,但 IBOutlet 为 nil。

所以你的代码应该是这样的

self.itemCollectionView.register(UINib(nibName: "NewSubscriptionRequestCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: "NewSubscriptionRequestCollectionReusableView", withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView")

registerNib:forCellWithReuseIdentifier: