ViewController 中的注册头:
self.itemCollectionView.register(NewSubscriptionRequestCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView")
在 CollectionView 委托(delegate)方法中:
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
}
您在注册可重用 View 时实现了错误的方法。您需要使用此方法注册您的 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:
关于ios - 如何访问从 UICollectionView 到 UICollectionReusableView 的网点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57832757/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |