2017-04-16 2 views
0

코드에서 사용하는 사용자 지정 UICollectionReusableView, HeaderCollectionReusableView가 있습니다. 그리고 차례로 UILabel이 포함됩니다. 라벨을 사용하려고 할 때 어떤 이유로 든 무효입니다. 우선 클래스를 등록한 다음 대기열에서 제거하려고합니다. 내가 뭘 놓치고 있니?UICollectionReusableView의 레이블은 항상 nil입니다.

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

를 사용하여. 제 경우에는 단순히 프로젝트를 정리하고 등록 호출없이 다시 빌드하는 것이 효과적이었습니다. –

답변

2

코드를 보면 사용자 지정 재사용 가능한보기가 xib를 사용하여 만들어지지 않는다고 가정합니다. 가장 좋은 옵션은

  1. xib를 사용하여 사용자 정의 재사용 뷰를 생성하십시오.
  2. 등록있는 viewDidLoad에서 그 명시 적으로 등록하는 HeaderCollectionReusableView가 원인이 된 전무 라벨 문제를 결국 다음과 같은 기능 즉 func register(_ nib: UINib?, forSupplementaryViewOfKind kind: String, withReuseIdentifier identifier: String)
관련 문제