2017-04-16 115 views
0

我在我的代码中使用了自定义的UICollectionReusableView,HeaderCollectionReusableView。而它又包含一个UILabel。当我去使用标签时,由于某种原因它是零。我首先注册了课程,然后我尝试用一​​个dequeue来调用它。我错过了什么?UICollectionReusableView内部的标签始终为零

override func viewDidLoad(){ 
    super.viewDidLoad() 

    collectionView?.register(HeaderCollectionReusableView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: colReusableViewIdentifier) 

    self.collectionView?.collectionViewLayout = UICollectionViewFlowLayout() 
} 

@objc override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    let headerReusableView = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: colReusableViewIdentifier, for: indexPath) as! HeaderCollectionReusableView 
    headerReusableView.headerLabel.text = "Reacshn" 

    return headerReusableView 
} 
+0

它结束了明确登记HeaderCollectionReusableView是导致无标签的问题。在我的情况下,简单地清理项目和重建没有注册电话工作。 –

回答

2

查看代码我假设自定义可重用视图不是使用xib创建的。最好的选择是

  1. 使用xib创建一个自定义的可重用视图。
  2. 注册在viewDidLoad中使用下面的函数,即func register(_ nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String)