【发布时间】:2020-01-09 23:11:09
【问题描述】:
ViewController 中的注册头:
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
}
我的 Header 集合文件
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)
}
}
我想从viewController 类更改pdfStatus 标签,但是当我试图通过以下代码实现时。它给了我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
}
【问题讨论】:
标签: ios swift uicollectionview swift4