2017-10-31 1 views
1

이 내가 헤더로 내 자신의 뷰를 추가 한 단계입니다은 어떻게 UICollectionView에 헤더보기를 추가하는

새보기 만들기 :

class HeaderView: UICollectionReusableView { 
    override init(frame: CGRect) { 
     super.init(frame: frame) 
    } 

    required init?(coder aDecoder: NSCoder) { 
     fatalError("init(coder:) has not been implemented") 
    } 

    override func layoutSubviews() { 
     super.layoutSubviews() 
     self.backgroundColor = .red 
    } 
} 

viewDidLoad 내부의 뷰를 등록이 :

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize { 
    return CGSize(width:view.frame.size.width, height:100.0) 
} 
:
self.collectionView?.register(HeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header") 

referenceSizeForHeaderInSection 대리자를 구현

viewForSupplementaryElementOfKind 위임 구현 :

func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
     switch kind { 
     case UICollectionElementKindSectionHeader: 
      let header = collectionView.dequeueReusableSupplementaryView(ofKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "header", for: indexPath) as! HeaderView 
      header.setNeedsLayout() 
      return header 

     default: 
      return UICollectionReusableView() 
     } 
    } 

을하지만 그것은 작동하지 않습니다. 내 referenceSizeForHeaderInSectionviewForSupplementaryElementOfKind은 앱을 실행할 때 호출되지 않습니다.

나는 viewDidLoad 내부 설정 내 대표는 않았다

self.collectionView?.delegate = self 

나는 어떤 단계를 실종? 아니면 내가 뭔가 잘못 했니?

편집 :

는 얘기를 깜빡 했네요 : 당신이 Section Header 옵션이 사라 레이아웃 사용자 정의 선택할 때이 문제가 있는지 확실하지 않습니다, 사용자 정의 레이아웃을 사용하지만, 스토리 보드에서하고 있습니다.

+0

컬렉션보기에 하나 이상의 섹션이 포함되어 있는지 확인하십시오. –

+0

예. 그것은 단지 하나의 섹션을 가지고 있습니다. 나는 내 데이터를 볼 수 있지만 헤더는 볼 수 없다. – Kobe

+0

'referenceSizeForHeaderInSection'은 레이아웃보기 대리자가 아닌 레이아웃 위임자의 메서드입니다. 레이아웃의 델리게이트도 설정 했습니까? –

답변

1

경우에 따라 섹션 헤더를 표시하려면 레이아웃을 올바르게 구성해야합니다.

맞춤 레이아웃을 사용하면 referenceSizeForHeaderInSection을 전혀 구현할 필요가 없습니다. UICollectionViewDelegateFlowLayout의 방법입니다. 그것이 호출되지 않는 이유입니다.

관련 문제