Reputation: 11052
Registered header in ViewController:
self.itemCollectionView.register(NewSubscriptionRequestCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView")
In the CollectionView Delegate Method:
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
}
My Header collection file
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)
}
}
I want to change the pdfStatus
label from the viewController
class but when I am trying to achieve through the following code. It's giving me 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
}
Upvotes: 2
Views: 658
Reputation: 1679
You implement wrong methods when you register your reusableView. You need to register your reusableView using this method.
collectionView.register(UINib, forSupplementaryViewOfKind:, withReuseIdentifier:)
Not
collectionView.register(Class, forSupplementaryViewOfKind:, withReuseIdentifier:)
If you don't register your XIB, UICollectionView don't have any data about IBOutlet(IBAction also). So Your cell can be created with a defined init method, but IBOutlet is nil.
So your code should be like
self.itemCollectionView.register(UINib(nibName: "NewSubscriptionRequestCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: "NewSubscriptionRequestCollectionReusableView", withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView")
registerNib:forCellWithReuseIdentifier:
Upvotes: 2