2017-05-22 3 views
1

헤더에 레이블을 삽입하려고했지만 collectionView를 스크롤하면 다른 레이블이보기에 추가됩니다. 내가 어떻게 그걸 막을 수 있니? enter image description hereCollectionView 헤더의 레이블 문제

override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView { 
    let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "header", for: indexPath) 
    header.backgroundColor = .white 
    let label = UILabel(frame: header.bounds) 
    label.text = colorsArray[indexPath.section] 
    header.addSubview(label) 
    return header 
} 

답변

1

당신은 헤더를 서브 클래스 화해,이 라벨을 추가해야합니다. 따라서 매번 머리글에 레이블을 추가하지는 않습니다.

모든 라벨을 제거하고 다시 추가하는 것은 매우 나쁜 습관과 큰 오버 헤드입니다.

+0

정말 헤더를 사용해야하므로 그렇게 할 수 없습니다. –

+0

하지만 UICollectionReusableView를 사용하여 헤더를 서브 클래스화할 수 있습니다. – Retterdesdialogs

+0

아, 죄송합니다. 'cell.label.text = "something"'나를 혼란스럽게합니다. 맞습니다. 잘 작동합니다. 감사 –