Notice
Recent Posts
Recent Comments
Link
ยซ   2024/07   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
๊ด€๋ฆฌ ๋ฉ”๋‰ด

lgvv98

ch11 ํ˜„์ƒ๊ธˆ ๋žญํ‚น์•ฑ ์ฝ”๋“œ๋ฆฌ๋ทฐ(CollectionView) ๋ณธ๋ฌธ

โš ๏ธ deprecated โš ๏ธ/ํŒจ์บ (์˜ฌ์ธ์›)

ch11 ํ˜„์ƒ๊ธˆ ๋žญํ‚น์•ฑ ์ฝ”๋“œ๋ฆฌ๋ทฐ(CollectionView)

๐Ÿฅ• ์บ๋Ÿฟ๋งจ 2021. 6. 22. 16:29

  โœ… CollectionView

 

Apple developer Document

https://developer.apple.com/documentation/uikit/uicollectionview

 

Apple Developer Documentation

 

developer.apple.com

 

์ด๋ฒˆ ์‹œ๊ฐ„์—๋Š” ๋“œ๋””์–ด CollectionView์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋‹ค.

ํ…Œ์ด๋ธ” ๋ทฐ๋Š” ์ž์‹  ์žˆ์—ˆ๋Š”๋ฐ, CollectionView๊ฐ€ ๋„ˆ๋ฌด๋‚˜๋„ ๊ถ๊ธˆํ–ˆ๋Š”๋ฐ, ๋“œ๋””์–ด ์‹œ์ž‘!!

 

โœ… ์ปฌ๋ ‰์…˜ ๋ทฐ๋„ ๊ฒฐ๊ตญ์€ ํ…Œ์ด๋ธ” ๋ทฐ์™€ ๋น„์Šทํ•œ ํ˜•์‹์„ ๋„๊ณ  ์žˆ๋‹ค.

โ—๏ธ์ฃผ์˜์‚ฌํ•ญโ—๏ธ

 - ์Šคํ† ๋ฆฌ๋ณด๋“œ์—์„œ CollectionView Controller๊ฐ€ ์•„๋‹Œ CollectionView๋ฅผ ์„ ํƒํ•ด์„œ UIView์— ๋„ฃ๋Š” ๋ฐฉ์‹์œผ๋กœ ๋งŒ๋“ค ์‹œ, CollectionView๋ฅผ Controller์— delegate๋ฐ dataSource๋ฅผ ์—ฐ๊ฒฐํ•ด์ค˜์•ผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

 - ์ด๋Š” ํ…Œ์ด๋ธ” ๋ทฐ์—์„œ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ, ๋‚ด๊ฐ€ ๊ฐ€๋” ์ด๊ฑธ ํ•˜์ง€ ์•Š์•„์„œ ํ—ค๋งค์ธ ์ ์ด ๋งŽ๋‹ค. ๊ผญ๊ผญ ๊ธฐ์–ตํ•˜์ž.

 

1๏ธโƒฃ UICollectionViewDataSource 

์—ฌ๊ธฐ์—๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ "๋ช‡๊ฐœ๋ฅผ ๋ณด์—ฌ์ค„๊ฑด์ง€?", "์…€์€ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ• ๊ฑด์ง€?"์— ๋Œ€ํ•ด์„œ ํ”„๋กœํ† ์ฝœ์„ ์ƒ์†๋ฐ›์•„์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.

๐Ÿ†š TableViewDataSource์™€์˜ ์ฐจ์ด์ 

1. indexPath.row๊ฐ€ ์•„๋‹Œ indexPath.item์„ ์‚ฌ์šฉํ•œ๋‹ค.

    // UICollectionViewDataSource
    // ๋ช‡๊ฐœ๋ฅผ ๋ณด์—ฌ์ค„๊นŒ์š”?
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return viewModel.numOfBountyInfoList
    }
    // ์…€์€ ์–ด๋–ป๊ฒŒ ํ‘œํ˜„ํ• ๊นŒ์š”?
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GridCell", for: indexPath) as? GridCell else {
            return UICollectionViewCell()
        }
        
        let bountyinfo = viewModel.bountyInfo(at: indexPath.item)
        cell.update(info: bountyinfo)
        
        return cell
    
    }

2๏ธโƒฃ UICollectionViewDelegate

์—ฌ๊ธฐ์—๋Š” "์…€์ด ํด๋ฆญ๋˜์—ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ํ• ๊ฑฐ์•ผ?" ๋ผ๊ณ  ๋‹ด๊ณ  ์žˆ๋‹ค.

๋ฉ”์†Œ๋“œ๋Š” TableView์™€ ๋น„์Šทํ•ด์„œ didSelectItemAt ์ด๋ผ๊ณ  ์ž‘์„ฑ๋˜์–ด ์žˆ์–ด์„œ ์ดํ•ดํ•˜๊ธฐ๊ฐ€ ์‰ฝ๋‹ค. 

    // UICollectionViewDelegate
    // ์…€์ด ํด๋ฆญ๋˜์—ˆ์„ ๋•Œ ์–ด๋–ป๊ฒŒ ํ• ๊นŒ์š”?
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("-->\(indexPath.item)")
        performSegue(withIdentifier: "showDetail", sender: indexPath.item)
    }

โญ๏ธ 3๏ธโƒฃ UICollectionViewDelegateFlowLayout

์ด ๋ถ€๋ถ„์€ ํ…Œ์ด๋ธ”๋ทฐ์™€ ๊ฐ€์žฅ ํฌ๊ฒŒ ๋‘๋“œ๋Ÿฌ์ง„ ์ฐจ์ด์ ์„ ๋ณด์—ฌ์ฃผ๋Š” ์ง€์ ์ด๋‹ค.

UICollectionView์˜ ๊ฒฝ์šฐ ํ…Œ์ด๋ธ” ๋ทฐ ํ˜•์‹์ด ์•„๋‹ˆ๋ผ ๊ฐ€๋กœ, ์„ธ๋กœ ๋“ฑ ์—ฌ๋Ÿฌ๊ฐœ์˜ ์•„์ดํ…œ์„ ๋ฐฐ์น˜ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์ด์— ๋”ฐ๋ผ์„œ ๋ ˆ์ด์•„์›ƒ์„ ๊ธฐ๊ธฐ์˜ ๋ณ€ํ™”์— ๋งž๊ฒŒ ๋Œ€์‘ํ•ด์ค˜์•ผ ํ•œ๋‹ค. 

์ด ๋ถ€๋ถ„์„ ์˜จ์ „ํžˆ ์ดํ•ดํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋ ˆ์ด์•„์›ƒ์— ๋Œ€ํ•œ ์ดํ•ด์™€ ๋”๋ถˆ์–ด ์ˆ˜ํ•™์  ์‚ฌ๊ณ ๊ฐ€ ์š”๊ตฌ๋œ๋‹ค.

์•„๋ž˜์˜ ์ฝ”๋“œ๋ฅผ ๊ฐ™์ด ๋ณด์ž.

    // UICollectionViewDelegateFlowLayout
    // ์…€์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ๋””๋ฐ”์ด์Šค ๋งˆ๋‹ค ์กฐ๊ธˆ์”ฉ ๋ณ€๊ฒฝ๋˜์–ด์•ผ ํ•  ํ•„์š”๊ฐ€ ์žˆ์–ด์„œ - ๊ฐ€๋กœํญ์ด ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์—
    // cell size ๊ณ„์‚ฐํ• ๊ฑฐ๋‹ค( ๋ชฉํ‘œ : ๋‹ค์–‘ํ•œ ๋””๋ฐ”์ด์Šค์—์„œ ์ผ๊ด€์ ์ธ ๋””์ž์ธ ๋ณด์—ฌ์ฃผ๊ธฐ ์œ„ํ•ด )
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        let itemSpacing : CGFloat = 10
        let textAreaHeight : CGFloat = 65
        
        let width : CGFloat = (collectionView.bounds.width - itemSpacing) / 2
        let height : CGFloat = width * 10/7 + textAreaHeight
        
        return CGSize(width: width, height: height)
    }

์œ„์˜ ์ฝ”๋“œ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋ ˆ์ด์•„์›ƒ์„ ์žก์œผ๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ์ด๋ฏธ์ง€์˜ ํ˜•ํƒœ๋ฅผ ๋„๊ฒŒ ๋œ๋‹ค. ๊ทธ๋Ÿผ ํ•œ์ค„ํ•œ์ค„ ์ฝ”๋“œ์— ๋Œ€ํ•ด์„œ ๋ฆฌ๋ทฐํ•ด ๋ณด์ž.

์œ„์˜ ์ฝ”๋“œ์— ๋”ฐ๋ผ ๋ ˆ์ด์•„์›ƒ์„ ์žก์€ ์ด๋ฏธ์ง€ - iphone 12 mini

 

โœ… ์šฐ์„  ์œ„์˜ ํ•จ์ˆ˜์—์„œ return ๊ฐ’์„ ๋ณด๋ฉด CGSize(width, height)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์žˆ๋Š”๋ฐ, ์ด๋ฅผ ํ…Œ์ด๋ธ” ์…€์˜ ๊ฐ ์•„์ดํ…œ ์ปจํ…Œ์ด๋„ˆ์˜ ํฌ๊ธฐ๋ผ๊ณ  ์ƒ๊ฐํ•˜์ž.

 

bounds์˜ ๊ฒฝ์šฐ์—๋Š” ์ž์‹ ๋งŒ์˜ ์ขŒํ‘œ๋ฅผ ๊ฐ€์ง

frame์˜ ์Šˆํผ ๋ทฐ(์ƒ์œ„ ๋ทฐ)๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ขŒํ‘œ๋ฅผ ๊ฐ€์ง

 

        let itemSpacing : CGFloat = 10 // ์•„์ดํ…œ ๊ฐ„์˜ ๊ฐ„๊ฒฉ
        let textAreaHeight : CGFloat = 65 // ๊ธ€์ž๊ฐ€ ์œ„์น˜ํ•  ๊ฐ„๊ฒฉ
        
        let width : CGFloat = (collectionView.bounds.width - itemSpacing) / 2 // ์ปฌ๋ ‰์…˜๋ทฐ์˜ ์ž์‹ ๋งŒ์˜ ์ž์‹ ์˜ ์ขŒํ‘œ๋ฅผ ๋„“์ด๋กœ ์‚ผ์•„, ์•„์ดํ…œ ๊ฐ„์˜ ๊ฐ„๊ฒฉ์„ ๋บด์ค€ ํ›„ ๋‚˜๋ˆ„๊ธฐ 2 

-> ์—ฌ๊ธฐ ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ๋ด์•ผํ•˜๋Š” ์ง€์ ์ธ๋ฐ, ๋‚˜๋ˆ„๊ธฐ 2ํ•˜๋Š” ์ด์œ ๋Š” ๊ธฐ๊ธฐ์˜ ์ „์ฒด ์•„์ดํ…œ์„ 2๊ฐœ ๋„ฃ์„ ์ƒ๊ฐ์ด๋ผ.

-> ์ด ๋ถ€๋ถ„์—์„œ ์ˆ˜ํ•™์ ์ธ ์‚ฌ๊ณ ๊ฐ€ ์š”๊ตฌ๋˜๋Š”๋ฐ, Q. ๋งŒ์•ฝ์— 3๊ฐœ๋ฅผ ๋„ฃ๊ณ  ์‹ถ์œผ๋ฉด ์ฝ”๋“œ๋ฅผ ์–ด๋–ป๊ฒŒ ์ˆ˜์ •ํ•ด์•ผ ํ• ๊นŒ?

-> A. let width : CGFloat = (collectionView.bounds.width - 2 * itemSpacing) / 3

       ์—ฌ๋ฐฑ ๊ณต๊ฐ„๋„ ์…€์ด ๋Š˜์–ด๋‚˜๋Š” ๋งŒํผ ์กฐ์ •๋˜์•ผ ํ•œ๋‹ค.

์•„์ดํ…œ์„ 3๊ฐœ ๋„ฃ์€ ํ›„ ์‹คํ–‰


        let height : CGFloat = width * 10/7 + textAreaHeight // width๊ฐ€ ์ •ํ•ด์กŒ์œผ๋ฉด, 7:10 ๋น„์œจ๋กœ ๋†’์ด๋ฅผ ์ •ํ•˜๊ณ , ๊ธ€์ž๊ฐ€ ๋“ค์–ด๊ฐˆ ๊ณต๊ฐ„๋งŒํผ ๋†’์ด๋ฅผ ๋” ํ™•๋ณดํ•œ๋‹ค.

โ—๏ธ์—ฌ๊ธฐ์„œ ์ฃผ์˜ํ•  ์  : :Label์„ ํฌ๊ธฐ๋ฅผ ๊ณ ๋ คํ•ด์„œ ์ ์ ˆํ•˜๊ฒŒ ์žก์•„์•ผ ํ•œ๋‹ค.
        
        return CGSize(width: width, height: height) // ๋งˆ์ง€๋ง‰์œผ๋กœ ์ด ๊ฐ’์„ ๋ฆฌํ„ดํ•˜๋ฉด ๋!

 

์—ฌ๊ธฐ๊นŒ์ง€๊ฐ€ UICollectionViewDelegateFlowLayout์— ๋Œ€ํ•œ ๊ธฐ๋ณธ์ ์ธ ์ •๋ณด์˜€๋‹ค. 
    

 

(์ถ”๊ฐ€)

โœ… ๊ฐ„ํ˜น ์šฐ๋ฆฌ๊ฐ€ ์ฝ”๋“œ๋ฅผ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์ž…๋ ฅํ–ˆ์Œ์—๋„ ๋ทฐ ๊ตฌ์„ฑ์ด ์ œ๋Œ€๋กœ ๋˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

โ—๏ธ์ด๊ฑด ํ”ํžˆํ•˜๋Š” ์‹ค์ˆ˜์ธ๋ฐ, ์šฐ๋ฆฌ๊ฐ€ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ• ๋•Œ, 

Esitimate Size

Estimate Size ๋ถ€๋ถ„์ด ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜คํ† ๋งคํ‹ฑ์œผ๋กœ ์„ค์ •๋˜์–ด ์žˆ๋Š”๋ฐ, ์ด ๋ถ€๋ถ„์€ ๋…ผ์œผ๋กœ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผ ํ•œ๋‹ค.

 

 - ์ฐธ๊ณ 

https://zeddios.tistory.com/203

 

Comments