2017-04-16 7 views
0

私は自分のコードで使用しているカスタムUICollectionReusableView、HeaderCollectionReusableViewを持っています。それにはUILabelも含まれています。私はラベルを使用するために行くとき、それは何らかの理由でnilです。最初にクラスを登録してからデキューして呼び出そうとします。私は何が欠けていますか?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

を使用してのviewDidLoadで登録するには、明示的にHeaderCollectionReusableViewを登録すると、nilのラベルの問題を引き起こしていたことを終わりました。私の場合、単にプロジェクトをきれいにして、登録呼び出しなしで再ビルドするだけでした。 –

答えて

2

コードを見る私は、カスタムの再利用可能なビューがxibを使用して作成されていないと仮定します。最適なオプションは

  1. xibを使用して再利用可能なカスタムビューを作成します。
  2. 次関数すなわちfunc register(_ nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String)
関連する問題