【问题标题】:How to access outlets from UICollectionView to UICollectionReusableView?如何访问从 UICollectionView 到 UICollectionReusableView 的网点?
【发布时间】: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


    【解决方案1】:

    您在注册可重用视图时实现了错误的方法。您需要使用此方法注册您的 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:

    【讨论】:

    • 感谢您的回复,但在替换为 UINIB 之后。我得到:'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindSectionHeader with identifier NewSubscriptionRequestCollectionReusableView - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
    • @AmitPal 您是如何创建您的 XIB 的? File > New File > View(User Interface)File > New File > Cocoa Touch Class
    • 还有一件事,你是如何为你的 ReusableView 设置重用标识符的?
    • 我使用了这段代码,它对我有用:` let headerNib = UINib.init(nibName: 'NewSubscriptionRequestCollectionReusableView', bundle: nil) itemCollectionView.register(headerNib, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "NewSubscriptionRequestCollectionReusableView")` 还有一个问题。我可以在viewForSupplementaryElementOfKind 中访问label,但是如果我必须从另一个地方访问相同的标签怎么办
    • 如果我在其他地方使用headerView.pdfStatus.text = "dadw"headerview = NewSubscriptionRequestCollectionReusableViewviewForSupplementaryElementOfKind.header 等价于viewForSupplementaryElementOfKind.header
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    • 2013-05-10
    • 1970-01-01
    • 2019-12-15
    • 1970-01-01
    • 2017-06-05
    相关资源
    最近更新 更多