ios - 如何访问从 UICollectionView 到 UICollectionReusableView 的网点?
<p><p>ViewController 中的注册头:</p>
<pre><code>self.itemCollectionView.register(NewSubscriptionRequestCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView")
</code></pre>
<p>在 CollectionView 委托(delegate)方法中:</p>
<pre><code>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
}
</code></pre>
<p>我的 Header 集合文件</p>
<pre><code>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)
}
}
</code></pre>
<p>我想从 <code>viewController</code> 类中更改 <code>pdfStatus</code> 标签,但是当我尝试通过以下代码实现时。它给了我 <code>nil</code></p>
<pre><code>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
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>您在注册可重用 View 时实现了错误的方法。您需要使用此方法注册您的 reusableView。</p>
<pre class="lang-swift prettyprint-override"><code>collectionView.register(UINib, forSupplementaryViewOfKind:, withReuseIdentifier:)
</code></pre>
<p>没有</p>
<pre class="lang-swift prettyprint-override"><code>collectionView.register(Class, forSupplementaryViewOfKind:, withReuseIdentifier:)
</code></pre>
<p>如果你没有注册你的 XIB,UICollectionView 没有任何关于 IBOutlet(IBAction 也是)的数据。因此,您的单元格可以使用定义的 init 方法创建,但 IBOutlet 为 nil。</p>
<p>所以你的代码应该是这样的</p>
<pre class="lang-swift prettyprint-override"><code>self.itemCollectionView.register(UINib(nibName: "NewSubscriptionRequestCollectionReusableView", bundle: nil), forSupplementaryViewOfKind: "NewSubscriptionRequestCollectionReusableView", withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView")
</code></pre>
<p> <a href="https://developer.apple.com/documentation/uikit/uicollectionview/1618083-registernib?language=objc" rel="noreferrer noopener nofollow">registerNib:forCellWithReuseIdentifier:</a> </p></p>
<p style="font-size: 20px;">关于ios - 如何访问从 UICollectionView 到 UICollectionReusableView 的网点?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/57832757/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/57832757/
</a>
</p>
页:
[1]