โœ๏ธ TIL

[ TIL ] ๋ณธ๊ฒฉ์ ์œผ๋กœ ํŒ€ํ”Œ์„ ์‹œ์ž‘ํ•ด๋ณด์ฃ ! / Collection View / 24๋…„ 4์›” 1์ผ

EarthSea 2024. 4. 1. 23:52

 

 

์•ˆ๋…•ํ•˜์„ธ์š”! ์˜ค๋Š˜๋ถ€ํ„ฐ ๋ณธ๊ฒฉ์ ์ธ ํŒ€ํ”Œ์ด ์‹œ์ž‘๋˜์—ˆ์Šต๋‹ˆ๋‹ค๐Ÿ

ํŒ€์›๋“ค๊ณผ ์นดํŽ˜ ์•ฑ์„ ๋งŒ๋“ค์–ด ๋ณด๊ธฐ๋กœ ํ–ˆ์–ด์š”~!

 

ํŒ€์›๋“ค๊ณผ ์•„์นจ๋ถ€ํ„ฐ UI๋ฅผ ์งœ๊ณ , ์–ด๋–ค ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•  ์ง€์— ๋Œ€ํ•œ ์ด์•ผ๊ธฐ๋ฅผ ๋‚˜๋ˆˆ ํ›„์—

์—ญํ• ์„ ๋‚˜๋ˆ„๊ณ  ๋ณธ๊ฒฉ์ ์ธ ๊ตฌํ˜„์— ๋“ค์–ด๊ฐ”์Šต๋‹ˆ๋‹ค.

 

์ €๋Š” ํ•œ๋ฒˆ๋„ ์‚ฌ์šฉํ•ด๋ณด์ง€ ์•Š์€ CollectionView๋ฅผ ๋งŒ์ ธ๋ณด๊ณ  ์‹ถ์–ด์„œ

์ ๊ทน์ ์œผ๋กœ ํ•˜๊ณ  ์‹ถ๋‹ค๋Š” ์˜๊ฒฌ์„ ๋‚ด์—ˆ์–ด์š”!!

๊ฐ์‚ฌํ•˜๊ฒŒ๋„ ๋‹ค๋ฅธ ํŒ€์›๋ถ„์ด ์–‘๋ณด๋ฅผ ํ•ด์ฃผ์…”์„œ CollectionView ํŒŒํŠธ๋ฅผ ๋งก๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค~!

 

 

CollectionView

CollectionView๋Š” ์•„๋ž˜์˜ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ View๋“ค์„ ์นด๋“œํ˜•์‹์œผ๋กœ ๋‚˜์—ดํ•  ์ˆ˜ ์žˆ๋Š” ๋ทฐ์ž…๋‹ˆ๋‹ค.

์ถœ์ฒ˜ : ์ธ์Šคํƒ€๊ทธ๋žจ ๊ณต์ฐจ ๊ณต์‹ ๊ณ„์ •

 

TableView์™€ ๋น„์Šทํ•˜๊ฒŒ Delegate์™€ DataSouce๋ฅผ ์ฑ„ํƒํ•˜์—ฌ CollectionView Cell๊ณผ CollectionView๊ฐ€ ์„œ๋กœ ์†Œํ†ต์„ ํ•˜๋Š” ํ˜•์‹์œผ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋”๋ผ๊ตฌ์š”!

 

 

UICollectionViewDataSource ํ”„๋กœํ† ์ฝœ

์€ CollectionView ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ณตํ•˜๋Š” ๋ฉ”์„œ๋“œ์™€ ํ•ด๋‹น ๋ฐ์ดํ„ฐ๋ฅผ CollectionView์— ํ‘œ์‹œํ•˜๋Š” ๋ฉ”์„œ๋“œ๋“ค์„ ๋‹ค๋ฃน๋‹ˆ๋‹ค.

 

[ ๊ด€๋ จ๋œ ํ•จ์ˆ˜ ]

๋”๋ณด๊ธฐ
  • collectionView(_:numberOfItemsInSection: ) : CollectionView์— ํ‘œ์‹œํ•œ Section์— ์†ํ•œ cell์˜ ๊ฐœ์ˆ˜ ๋ฐ˜ํ™˜
  • collectionView(_:cellForItemAt: ) : ํŠน์ • ์œ„์น˜์— ํ‘œ์‹œํ•œ cell ๋ฐ˜ํ™˜ ( cell์„ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  data๋ฅผ ์„ค์ •ํ•จ ) 

 

 

UICollectionViewDelegate ํ”„๋กœํ† ์ฝœ

์€ ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค์™€ ๊ด€๋ จ๋œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๋‹ค๋ค„์š”. ์˜ˆ๋ฅผ ๋“ค๋ฉด, CollectionView์˜ ๋ ˆ์ด์•„์›ƒ ์„ค์ •์ด๋‚˜ cell์ด ์„ ํƒ๋œ ํ›„์˜ ๋™์ž‘, scroll์ด๋‚˜ animation ๊ด€๋ฆฌ ๋“ฑ์ด ์žˆ์ฃ .

 

[ ๊ด€๋ จ๋œ ํ•จ์ˆ˜ ]

๋”๋ณด๊ธฐ
  • collectionView(_:didSelectItemAt:) : ํŠน์ • cell์„ ์„ ํƒํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ
  • collectionView(_:didDeselectItemAt:) : ์„ ํƒํ•œ cell์„ ์„ ํƒํ•ด์ œํ–ˆ์„ ๋•Œ ํ˜ธ์ถœ
  • collectionView(_:willDisplay:forItemAt:) : ํŠน์ • cell์„ ํ‘œ์‹œํ•˜๊ธฐ ์ „์— ํ˜ธ์ถœ
  • collectionView(_:didEndDisplaying:forItemAt:) : ํŠน์ • cell์ด ํ™”๋ฉด์—์„œ ์‚ฌ๋ผ์ง„ ํ›„ ํ˜ธ์ถœ

 

 

CollectionView๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์œ„ ๋‘ ํ”„๋กœํ† ์ฝœ์„ ๋ฐ˜๋“œ์‹œ ์ฑ„ํƒํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ ํ•„์ˆ˜ ์ฑ„ํƒ์€ ์•„๋‹ˆ์ง€๋งŒ, ๊ตฌํ˜„์— ์žˆ์–ด์„œ ํ•„์š”ํ•œ ํ”„๋กœํ† ์ฝœ๋„ ์†Œ๊ฐœํ•ด ๋“œ๋ฆด๊ฒŒ์š”!

 

 

UICollectionViewDelegateFlowLayout ํ”„๋กœํ† ์ฝœ

์€ CollectionView์˜ ๋ ˆ์ด์•„์›ƒ๊ณผ ๊ด€๋ จ๋œ ๋ถ€๋ถ„๋“ค์„ ์ œ์–ดํ•  ์ˆ˜ ์žˆ์–ด์š”. cell์˜ ํฌ๊ธฐ๋‚˜ ๊ฐ„๊ฒฉ ๋˜๋Š” ๊ทธ๋ฆฌ๋“œ์˜ ํฌ๊ธฐ๋‚˜ ๊ฐ„๊ฒฉ ๋“ฑ์„ ์กฐ์ ˆํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

[ ๊ด€๋ จ๋œ ํ•จ์ˆ˜ ]

๋”๋ณด๊ธฐ
  • collectionView(_:layout:sizeForItemAt:) : ๊ฐœ๋ณ„ ์…€์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • collectionView(_:layout:insetForSectionAt:) : Section์˜ ์—ฌ๋ฐฑ์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
  • collectionView(_:layout:minimumLineSpacingForSectionAt:) : Section์˜ ์ค„ ๊ฐ„๊ฒฉ์„ ๋ฐ˜ํ™˜
  • collectionView(_:layout:minimumInteritemSpacingForSectionAt:) : Section์˜ ํ•ญ๋ชฉ ๊ฐ„๊ฒฉ์„ ๋ฐ˜ํ™˜
  • collectionView(_:layout:referenceSizeForHeaderInSection:) : Header์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜
  • collectionView(_:layout:referenceSizeForFooterInSection:) : Footer์˜ ํฌ๊ธฐ๋ฅผ ๋ฐ˜ํ™˜

 

 

 

์ €๋Š” ์˜ค๋Š˜ ์—ฌ๊ธฐ์„œ 4๊ฐ€์ง€ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

 

CollectionView ๊ตฌํ˜„

 

[ ViewController ]

1. ์Šคํ† ๋ฆฌ ๋ณด๋“œ์—์„œ CollectinoView ์˜คํ† ๋ ˆ์ด์•„์›ƒ ์žก๊ธฐ

 

2. CollectionViewCell ํŒŒ์ผ ์ƒ์„ฑํ•˜์—ฌ CollectionCell์™€ ์—ฐ๊ฒฐํ•˜๊ธฐ

 

3. ํ”„๋กœํ† ์ฝœ ์ฑ„ํƒ

extension MenuListViewController: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {

}

 

4. delegate์™€ datasource ๋ชฉ์ ์ง€ ์ž๊ธฐ ์ž์‹ ์œผ๋กœ ์„ค์ •

    // collectionView setting
    func setCollectionView() {
        menuCollectionView.dataSource = self
        menuCollectionView.delegate = self
    }

์œ„์˜ ํ•จ์ˆ˜๋Š” viewDidLoad์— ๋„ฃ์–ด์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค!

 

5. collectionView์— ๋ฐ˜ํ™˜ํ•  cell์˜ ๊ฐœ์ˆ˜ ๋ฆฌํ„ดํ•˜๊ธฐ

    // collectionView cell ๊ฐœ์ˆ˜
    func collectionView(_ collectionView: UICollectionView,
                        numberOfItemsInSection section: Int) -> Int {
        return menuData.count
    }

 

6. collectionView์™€ cell ์—ฐ๊ฒฐํ•˜๊ณ , Data ์ „๋‹ฌํ•˜๊ธฐ

    // collectionView์™€ cell ์—ฐ๊ฒฐ / menuData ์ „๋‹ฌ
    func collectionView(_ collectionView: UICollectionView,
                        cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MenuCell", for: indexPath) as? MenuListCollectionViewCell else {
            return UICollectionViewCell()
        }
        
        cell.setConfigue(menuData[indexPath.row])
        
        return cell
    }

 

7. collectionView ํŠน์ • cell ์„ ํƒ์‹œ cart์— ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ํ•˜๊ธฐ

    // collectionView ์„ ํƒ์‹œ cart์— ์ถ”๊ฐ€
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        cartDataManager.addCartData(name: menuData[indexPath.row].name)
    }

 

8. collectionView์˜ view ์‚ฌ์ด์ฆˆ ์„ค์ •ํ•˜๊ธฐ

    // collectionView์˜ view ์‚ฌ์ด์ฆˆ ์„ค์ •
    func collectionView(_ collectionView: UICollectionView,
                        layout collectionViewLayout: UICollectionViewLayout,
                        sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 170, height: 170)
    }

 

9. collectionView์˜ ํ–‰ ๊ฐ„๊ฒฉ ์„ค์ •ํ•˜๊ธฐ

    // collectionView์˜ ํ–‰ ๊ฐ„๊ฒฉ ์„ค์ •
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 17
    }

 

 

[ CollectionViewCell ]

1. cell์˜ ์Šคํ† ๋ฆฌ๋ณด๋“œ์™€ @IBOutlet ์—ฐ๊ฒฐํ•˜๊ธฐ

    @IBOutlet weak var menuImageView: UIImageView!
    @IBOutlet weak var menuPriceLabel: UILabel!
    @IBOutlet weak var menuNameLabel: UILabel!

 

2. ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋กœ ๋ฉ”๋‰ด ์ด๋ฏธ์ง€, ๋ฉ”๋‰ด ๊ฐ€๊ฒฉ, ๋ฉ”๋‰ด ์ด๋ฆ„ ์„ค์ •ํ•˜๊ธฐ

    func setConfigue(_ data: MenuData){
        
        // ์ด๋ฏธ์ง€๊ฐ€ ์žˆ๋‹ค๋ฉด ์„ค์ •
        if let image = data.image {
            menuImageView.image = image
        }else {
            menuImageView.image = UIImage(named: "mug")
            menuImageView.tintColor = .black
        }
        
        // ๋ฉ”๋‰ด ๊ฐ€๊ฒฉ ์„ค์ •
        menuPriceLabel.text = String(data.price) + "์›"
        
        // ๋ฉ”๋‰ด ์ด๋ฆ„ ์„ค์ •
        menuNameLabel.text = data.name
   }

 

3. contentView์˜ ํ…Œ๋‘๋ฆฌ ์„ค์ •ํ•˜๊ธฐ

// ํ…Œ๋‘๋ฆฌ ์„ค์ •
// ๋‘ฅ๊ทผ ๋ชจ์„œ๋ฆฌ
contentView.layer.cornerRadius = 16
contentView.clipsToBounds = true
// ๊ฒฝ๊ณ„์„  ํฌ๊ธฐ
contentView.layer.borderWidth = 1
// ๊ฒฝ๊ณ„์„  ์ƒ‰๊น”
contentView.layer.borderColor = UIColor(red: 128/255, green: 202/255, blue: 255/255, alpha: 1.0).cgColor

 

๊ตฌํ˜„ ํ™”๋ฉด

 

์šฐ์„  ์ €์—๊ฒŒ ์ฃผ์–ด์ง„ ์ž„๋ฌด๋Š” ์™„๋ฃŒํ•œ ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!!ใ…Žใ…Ž

 

์ปฌ๋ ‰์…˜ ๋ทฐ ์˜ ์ด์ง€ ํ•ฉ๋‹ˆ๋‹ค! ๋ผ๊ณ  ๋งํ•˜๊ณ  ์‹ถ์ง€๋งŒ, ์˜ค๋Š˜ ํฌ๋‚˜ํฐ ์˜ค๋ฅ˜์— ๋ง‰ํ˜”์—ˆ์–ด์š”..

์˜ค๋ฅ˜ ํ•ด๊ฒฐ ๊ณผ์ •

Thread 1: "[<UICollectionViewCell 0x105c08420> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key menuBackgroundView."

 

ํ•ด์„ํ•ด๋ณด๋‹ˆ ์ด ํด๋ž˜์Šค ( UICollectionViewCell )์€ menuBackgroundView ์— ๋Œ€ํ•œ key value Colding-compliant๊ฐ€ ์•„๋‹ˆ๋ž˜์š”..

 

 

1์‹œ๊ฐ„์ •๋„์˜ ์‚ฝ์งˆ ํ›„์— ์ด๊ฒƒ ์ €๊ฒƒ ๋‹ค ๋งŒ์ ธ๋ณด๋‹ค๊ฐ€ ์•Œ์•„๋‚ธ ์‚ฌ์‹ค์€ ์ œ๊ฐ€ ์ € Module๊ฐ’์„ ์‚ญ์ œํ–ˆ๋”๋ผ๊ตฌ์š”.. Class๋ฅผ ๋ณ€๊ฒฝํ•˜๋‹ค๊ฐ€ ์ž˜๋ชป ๋ˆŒ๋Ÿฌ์„œ ์›๋ž˜ ๋น„์–ด์žˆ๋Š” ๋ถ€๋ถ„์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ์ง€์šฐ๊ณ  ๋น ์ ธ๋‚˜์™”๋Š”๋ฐ,, ์•Œ๊ณ ๋ณด๋‹ˆ ์ € Module์€ ์ด Class๊ฐ€ ์กด์žฌํ•˜๋Š” ์œ„์น˜๋ฅผ ์ด์•ผ๊ธฐํ•˜๊ณ  ์žˆ๋Š” ๊ฑฐ๋”๋ผ๊ตฌ์š”! ๊ทธ๋ ‡๊ฒŒ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.. ํ›„.. ์ด๋ฒˆ์— ์‚ฝ์งˆํ–ˆ์œผ๋‹ˆ ๋‹ค์Œ๋ฒˆ์—” ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ๊ฒ ์ฃ ?!ใ…Ž 

 

Module์€ ํ”„๋ ˆ์ž„์›Œํฌ๊ฐ€ ์•ฑ์˜ ๋…๋ฆฝ์ ์ธ ๋ถ€๋ถ„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. Class์˜ ์ด๋ฆ„์ด ๋ฌด์ˆ˜ํžˆ ๋งŽ์ง€๋งŒ ํ•ด๋‹น Module์— ์†ํ•œ class๋Š” ๋‹จ ํ•œ ๊ฐœ์ด๋ฏ€๋กœ Xcode๊ฐ€ ์–ด๋”” MenuListCollectionView์ธ์ง€ ์ž˜ ๊ตฌ๋ถ„ํ•ด๋‚ผ ์ˆ˜ ์žˆ๊ฒ ์ฃ ! "Inherit Module From Target" ๋ผ๋Š” ์˜ต์…˜์€ ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ ์†ํ•œ ๋ชจ๋“ˆ์ด๋‚˜ ํƒ€๊ฒŸ์„ ์ƒ์†ํ•œ๋‹ค๋Š” ์˜๋ฏธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ์ €๊ฑธ ์ฒดํฌํ•˜๋ฉด ์ง€๊ธˆ ํ˜„์žฌ ์†ํ•ด์žˆ๋Š” Module ์•ˆ์—์„œ์˜ Class๋ฅผ ์˜๋ฏธํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์ฒดํฌํ•ด์ œํ–ˆ๋‹ค๋ฉด ๋‹น์—ฐํžˆ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜๊ฒ ์ฃ ..!