Ios 如何将插座从UICollectionView访问到UICollectionReusableView?

Ios 如何将插座从UICollectionView访问到UICollectionReusableView?,ios,swift,uicollectionview,swift4,Ios,Swift,Uicollectionview,Swift4,ViewController中已注册的标题: self.itemCollectionView.register(NewSubscriptionRequestCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView") 在C

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
}
我的头集合文件

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
}

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

collectionView.register(UINib,用于种类为的Supplementary视图,带ReuseIdentifier:)
不是

collectionView.register(类,用于种类为的SupplementaryViewofKind:,带有ReuseIdentifier:)
如果您没有注册XIB,UICollectionView就没有关于IBOutlet(iAction)的任何数据。因此,可以使用定义的init方法创建单元格,但IBOutlet为nil

所以你的代码应该是

self.itemCollectionView.register(UINib(nibName:“NewSubscriptionRequestCollectionReusableView”,bundle:nil),用于“NewSubscriptionRequestCollectionReusableView”类的SupplmentViews,带有ReuseIdentifier:“NewSubscriptionRequestCollectionReusableView”)

感谢您的回复,但在用
UINIB
替换之后。我得到:
'nsinternalinconsistenceexception',原因:'无法将具有标识符NewSubscriptionRequestCollectionReusableView的类型的视图出列:UICollectionElementKindSectionHeader-必须为标识符注册nib或类,或连接序列图像板中的原型单元格'
@AmitPal您是如何创建XIB的<代码>文件>新文件>视图(用户界面)或
文件>新文件>Cocoa Touch类
还有一件事,您是如何为您的ReusableView设置重用标识的?我使用了这段代码,它对我有效:`let headerNib=UINib.init(nibName:'NewSubscriptionRequestCollectionReusableView',bundle:nil)itemCollectionView.register(headerNib,用于种类的SupplmentViewofKind:UICollectionView.elementKindSectionHeader,带有ReuseIdentifier:“NewSubscriptionRequestCollectionReusableView”)`再问一个问题。我可以访问
标签
视图,以查看同类的SupplmentalElementofKind
,但是如果我必须从另一个位置访问同一标签怎么办?如果我使用
headerView.pdfStatus.text=“dadw”如何
在其他地方,而
headerview=NewSubscriptionRequestCollectionReusableView
相当于
viewforsupplementalelementofkind.header