[TIL] 2021.02.24


👩🏻‍💻 今日の勉強内容


CollectionView Header

  • Collection ReusableViewクラス
  • 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 ()
        }
    }

    システムイメージに設定

    UIImage(systemName: "heart.fill")