[TIL] 2021.02.24

승아·2021년 2월 24일
0

👩🏻‍💻 오늘 공부한 내용

CollectionView Header

  • CollectionReusableView 클래스
class WishListHeaderView: UICollectionReusableView{
    @IBOutlet weak var titleLabel: UILabel!
    
    func updateUI(_ title: String){
        titleLabel.text = title
    }
}
  • 섹션 갯수
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 2
}
  • 각 섹션 별 아이템 수
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    if section == 0 {
        return wishListViewModel.favoriteWishs().count
    }
    else {
        return wishListViewModel.wishs.count
    }
}
  • 섹션 별 표현할 셀
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    if indexPath.section == 0 {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WishListCell", for: indexPath) as? WishListCell else {
            return UICollectionViewCell()
        }
        
        return cell
    }
    else {
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "WishListCell", for: indexPath) as? WishListCell else {
            return UICollectionViewCell()
        }
        
        return cell
    }
}
  • 섹션 별 헤더 설정
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    switch kind {
    case UICollectionView.elementKindSectionHeader:
        guard let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "WishListHeaderView", for: indexPath) as? WishListHeaderView else {
            return UICollectionReusableView()
        }
        
        if indexPath.section == 0{
            header.updateUI("즐겨찾는 Wish")
        }
        else {
            header.updateUI("나의 Wish")
        }
        
        return header
    default:
        return UICollectionReusableView ()
    }
}

SystemImage로 설정

UIImage(systemName: "heart.fill")

0개의 댓글