์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
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 |
- UIKit
- raywenderlich
- TCA
- realm
- BFS
- MVVM
- swift
- designpattern
- CollectionView
- Lv2
- ๋ฐฑ์ค
- Flutter
- Swfit
- BOJ
- visionOS
- combine
- reactorkit
- RxSwift
- Kuring
- rxcocoa
- Xcode
- SnapKit
- ํ๋ก๊ทธ๋๋จธ์ค
- arkit
- node.js
- SwiftUI
- XCTest
- ํจ์คํธ์บ ํผ์ค
- ios
- tableView
- Today
- Total
lgvv98
ch11 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(CollectionView) ๋ณธ๋ฌธ
ch11 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(CollectionView)
๐ฅ ์บ๋ฟ๋งจ 2021. 6. 22. 16:29โ CollectionView
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)
}
์์ ์ฝ๋๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ ์ด์์์ ์ก์ผ๋ฉด ์๋์ ๊ฐ์ ์ด๋ฏธ์ง์ ํํ๋ฅผ ๋๊ฒ ๋๋ค. ๊ทธ๋ผ ํ์คํ์ค ์ฝ๋์ ๋ํด์ ๋ฆฌ๋ทฐํด ๋ณด์.
โ ์ฐ์ ์์ ํจ์์์ 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
์ฌ๋ฐฑ ๊ณต๊ฐ๋ ์ ์ด ๋์ด๋๋ ๋งํผ ์กฐ์ ๋์ผ ํ๋ค.
let height : CGFloat = width * 10/7 + textAreaHeight // width๊ฐ ์ ํด์ก์ผ๋ฉด, 7:10 ๋น์จ๋ก ๋์ด๋ฅผ ์ ํ๊ณ , ๊ธ์๊ฐ ๋ค์ด๊ฐ ๊ณต๊ฐ๋งํผ ๋์ด๋ฅผ ๋ ํ๋ณดํ๋ค.
โ๏ธ์ฌ๊ธฐ์ ์ฃผ์ํ ์ : :Label์ ํฌ๊ธฐ๋ฅผ ๊ณ ๋ คํด์ ์ ์ ํ๊ฒ ์ก์์ผ ํ๋ค.
return CGSize(width: width, height: height) // ๋ง์ง๋ง์ผ๋ก ์ด ๊ฐ์ ๋ฆฌํดํ๋ฉด ๋!
์ฌ๊ธฐ๊น์ง๊ฐ UICollectionViewDelegateFlowLayout์ ๋ํ ๊ธฐ๋ณธ์ ์ธ ์ ๋ณด์๋ค.
(์ถ๊ฐ)
โ ๊ฐํน ์ฐ๋ฆฌ๊ฐ ์ฝ๋๋ฅผ ์ฌ๋ฐ๋ฅด๊ฒ ์ ๋ ฅํ์์๋ ๋ทฐ ๊ตฌ์ฑ์ด ์ ๋๋ก ๋์ง ์๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
โ๏ธ์ด๊ฑด ํํํ๋ ์ค์์ธ๋ฐ, ์ฐ๋ฆฌ๊ฐ ์ฝ๋๋ฅผ ์์ฑํ ๋,
Estimate Size ๋ถ๋ถ์ด ๊ธฐ๋ณธ์ ์ผ๋ก ์คํ ๋งคํฑ์ผ๋ก ์ค์ ๋์ด ์๋๋ฐ, ์ด ๋ถ๋ถ์ ๋ ผ์ผ๋ก ๋ฐ๊ฟ์ฃผ์ด์ผ ํ๋ค.
- ์ฐธ๊ณ
https://zeddios.tistory.com/203
'โ ๏ธ deprecated โ ๏ธ > ํจ์บ (์ฌ์ธ์)' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ch12 ์ ํ๋ฎค์งst ์์ ์ฑ ์ฝ๋๋ฆฌ๋ทฐ (0) | 2021.06.24 |
---|---|
๐ ch11 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(Animation)๐ (0) | 2021.06.22 |
๐ ch11 Animation - ๊ฐ๋ ํธ๐ (0) | 2021.06.22 |
ch10 ํ์๊ธ ๋ญํน์ฑ ์ฝ๋๋ฆฌ๋ทฐ(MVVM) (0) | 2021.06.21 |
ch10 MVVM ํจํด (0) | 2021.06.21 |