๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŽ iOS

[Swift] UICollectionView์—์„œ Section์„ ๋‚˜๋ˆ  ๋ณด์ž!

by ํ‹ด๋”” 2020. 9. 28.
728x90
๋ฐ˜์‘ํ˜•

์ค€๋น„๋ฌผ & ๋“ค์–ด๊ฐ€๋ฉฐ

์ด์ „ ํฌ์ŠคํŒ…์— ์ด์–ด์„œ ์ž‘์—…ํ•˜๋Š” ํŠœํ† ๋ฆฌ์–ผ ์ด์ง€๋งŒ ์ด๋ฏธ ์ž‘์„ฑ๋œ ๊ธฐ๋ณธ UICollectionView๋งŒ ์žˆ๋‹ค๋ฉด ์กฐ๊ธˆ์ด๋‚˜๋งˆ ๋„์›€์ด ๋  ๊ฑฐ ๊ฐ™์Šต๋‹ˆ๋‹ค! ์ด๋ฒˆ์— ๊ตฌํ˜„ํ•  ๊ฒƒ์€ UICollectionView๋ฅผ 3๊ฐœ์˜ Section์œผ๋กœ ๋‚˜๋ˆ„๋Š” ๊ฒƒ ์ž…๋‹ˆ๋‹ค~ (์„น์…˜์— ๋“ค์–ด๊ฐ€๋Š” ์…€์€ ๋‹ค์Œ ํฌ์ŠคํŒ…์—์„œ ๋‹ค๋ฃจ๊ฒ ์Šต๋‹ˆ๋‹ค.)

๊ฐ€๋” UI ํŠœํ† ๋ฆฌ์–ผ ๋ณด๋ฉด ์ƒ๋‹จ ์ปฌ๋ ‰์…˜ ๋ทฐ๋Š” ์ž๋™์œผ๋กœ ๋„˜์–ด๊ฐ€๋Š” ์นด๋“œ๋ทฐ, ๋ฐ”๋กœ ์•„๋ž˜์—๋Š” ๋‹ค๋ฅธ ์ปฌ๋ ‰์…˜ ๋ทฐ๋ฅผ ๋†“๋Š” ๋‹ค ๋˜๊ฐ€, ๊ฐ™์€ ํ™”๋ฉด์—์„œ ๋‹ค๋ฅธ ์ปฌ๋ ‰์…˜ ๋ทฐ๋“ค์ด ๋“ค์–ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

์ด๋•Œ ๊ฐ๊ฐ์˜ ์ปฌ๋ ‰์…˜ ๋ทฐ๋ฅผ ๊ตฌํ˜„ํ•ด๋„ ๋˜๊ฒ ์ง€๋งŒ Section์„ ๋‚˜๋ˆ„์–ด ๊ตฌํ˜„ํ•ด๋„ ๋ฉ๋‹ˆ๋‹ค~ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ด…์‹œ๋‹ค~~


section์˜ ๊ฐฏ์ˆ˜๋ฅผ ์ง€์ •ํ•˜๋Š” ๋ฉ”์†Œ๋“œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

optional func numberOfSections(in collectionView: UICollectionView) -> Int

 optional์ด๊ธฐ ๋•Œ๋ฌธ์— ์ž‘์„ฑํ•˜์ง€ ์•Š์•„๋„ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

3๊ฐœ์˜ ์„น์…˜์„ ์ƒ์„ฑํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค

func numberOfSections(in collectionView: UICollectionView) -> Int {
    3
}

so simple

 

๊ทธ๋ฆฌ๊ณ  ๊ฐ ์„น์…˜์— ํ•œ ๊ฐœ์˜ ์…€๋งŒ ๋„ฃ์–ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    1
}

์ปค์Šคํ…€ ์…€์„ ๋„ฃ๊ธฐ ์œ„ํ•ด ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

cocoa Touch class ์„ ํƒํ•ด ์ฃผ์‹œ๊ณ 

Subclass ๋ฅผ UICollectionViewCell๋กœ ์ง€์ •ํ•ด ์ฃผ์„ธ์š”

class ๋ช…์€ ์›ํ•˜์‹œ๋Š” ๊ฑธ๋กœ ์ง€์œผ์‹œ๋ฉด ๋ฉ๋‹ˆ๋‹ค

XIB๋ฅผ ์ด์šฉํ•ด์„œ UI๋ฅผ ์ž‘์„ฑํ•˜๊ณ  ์‹ถ์œผ์‹œ๋‹ค๋ฉด 'Also create XIB file'์— ์ฒดํฌํ•ด ์ฃผ์‹œ๋ฉด

xib ํŒŒ์ผ์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค

์ €๋Š” ๊ฐ„๋‹จํ•œ ์…€์„ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋„˜์–ด๊ฐ€ ์ฃผ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์„น์…˜์„ ์ด 3๊ฐœ ๋งŒ๋“ค์—ˆ๊ณ  ๊ฐ ์„น์…˜์— ๋”ฐ๋ฅธ UICollectionViewCell ํŒŒ์ผ์„ ์ƒ์„ฑํ•ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

๊ฐ UICollectionViewCell์„ ์•Œ์•„ ๋ณผ ์ˆ˜ ์žˆ๋„๋ก ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ ํŒŒ์ผ์— ์•„๋ž˜์™€ ๊ฐ™์ด ์ ์–ด์ค๋‹ˆ๋‹ค

์ƒ‰์ƒ์€ ๋‹ค ๋‹ค๋ฅด๊ฒŒ ์ž‘์„ฑํ•ด ์ฃผ์„ธ์š”

import UIKit

class MinutePickerCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = #colorLiteral(red: 0.5843137503, green: 0.8235294223, blue: 0.4196078479, alpha: 1)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

 

๋‹ค์‹œ ViewController.swift ํŒŒ์ผ๋กœ ๋Œ์•„์™€ ์ฃผ์„ธ์š”

์žฌ์‚ฌ์šฉ ์…€์— ๋“ฑ๋กํ•ด์•ผ ํ•  ๋•Œ ์‚ฌ์šฉํ•  identifier๋Š” ์…€์—์„œ ์ง€์ •ํ•œ ๊ฒƒ๊ณผ ์ปฌ๋ ‰์…˜ ๋ทฐ์— ๋“ฑ๋กํ•œ id์˜ ์ฒ ์ž๊ฐ€ ๋‹ค๋ฅด๋ฉด ์ œ๋Œ€๋กœ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ ์•„๋ž˜์™€ ๊ฐ™์ด identifier์„ ์ž‘์„ฑํ•ด ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
    
    let ID_DATE_CELL = "DatePickerCell"
    let ID_HOUR_CELL = "HourPickerCell"
    let ID_MIN_CELL = "MinutePickerCell"
    
    
    //(์ดํ•˜ ์ƒ๋žต)

์ด์ œ ์„น์…˜ ๋ณ„๋กœ cell์„ ์ง€์ •ํ•ด ์ค๋‹ˆ๋‹ค

cellForItemAt ๋ฉ”์†Œ๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        var cell = UICollectionViewCell()
        
        if indexPath.section == 0 {
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: ID_DATE_CELL, for: indexPath) as! DatePickerCell
        } else if indexPath.section == 1 {
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: ID_HOUR_CELL, for: indexPath) as! HourPickerCell
        } else {
            cell = collectionView.dequeueReusableCell(withReuseIdentifier: ID_MIN_CELL, for: indexPath) as! MinutePickerCell
        }
        
        return cell
    }

collectionView์— ํ•ด๋‹น ์…€์„ ๋“ฑ๋กํ•ด ์ค๋‹ˆ๋‹ค. viewDidLoad ๋ถ€๋ถ„์—์„œ

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(collectionView)
        
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        
        // ์ˆ˜์ •๋œ ๋ถ€๋ถ„ ----------------------------------------------------------------------------------------
        self.collectionView.register(DatePickerCell.self, forCellWithReuseIdentifier: ID_DATE_CELL)
        self.collectionView.register(HourPickerCell.self, forCellWithReuseIdentifier: ID_HOUR_CELL)
        self.collectionView.register(MinutePickerCell.self, forCellWithReuseIdentifier: ID_MIN_CELL)
        // ----------------------------------------------------------------------------------------
        self.collectionView.backgroundColor = #colorLiteral(red: 0.9568627477, green: 0.6588235497, blue: 0.5450980663, alpha: 1)
        
        
        let constraints = [
            collectionView.heightAnchor.constraint(equalToConstant: 180),
            collectionView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor),
            collectionView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor),
            collectionView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 300)
        ]
        
        NSLayoutConstraint.activate(constraints)
        
    }

๊ฐ ์…€์˜ ๋†’์ด์™€ ๋„“์ด๋ฅผ ์ง€์ •ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด ๋ฉ”์†Œ๋“œ๋ฅผ ์จ์ฃผ๊ฒ ์Šต๋‹ˆ๋‹ค.

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize

์ด ๋ฉ”์†Œ๋“œ๋Š” collectionView๋ฅผ ์ธ์ž๋กœ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์„œ ์ปฌ๋ ‰์…˜๋ทฐ์˜ ๋„“์ด์™€ ๋†’์ด๋„ ์•Œ ์ˆ˜ ์žˆ๊ณ  indexPath๋„ ์•Œ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์—

๊ฐ ์„น์…˜ ๋ณ„๋กœ ๊ฐ ์…€์˜ ๋†’์ด์™€ ๋„“์ด๋ฅผ ์ง€์ •ํ•ด ์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

์ €๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ๋„“์ด๋Š” ํ™”๋ฉด ๋„“์ด(์ปฌ๋ ‰์…˜ ๋ทฐ ๋„“์ด), ๋†’์ด๋Š” ๊ฐ ์…€๋‹น ์‚ผ๋ถ„์˜ ์ผ์”ฉ ํ•ด์„œ

๊ฒน์น˜์ง€ ์•Š๋„๋ก ์„น์…˜ ๊ตฌ์—ญ์„ ๋‚˜๋ˆ„์–ด ์ฃผ์—ˆ์Šต๋‹ˆ๋‹ค.

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: collectionView.frame.width, height: collectionView.frame.height/3)
    }

์‹คํ–‰ํ•ด ๋ณด๋ฉด

3๊ฐœ์˜ ์„น์…˜์— ๊ฐ ์…€์ด ์ง€์ •๋œ ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค!

 

ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์ด์ƒํ•œ ๋ถ€๋ถ„์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€ ๋ถ€ํƒ๋“œ๋ ค์š”! ์–ธ์ œ๋“ ์ง€ ํ™˜์˜์ž…๋‹ˆ๋‹ค~๐Ÿ˜Š

728x90
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€