Bibi's DevLog ๐Ÿค“๐ŸŽ

Collection View(UICollectionView) ์ปฌ๋ ‰์…˜๋ทฐ ๋ณธ๋ฌธ

๐Ÿ“ฑ๐ŸŽ iOS/๐Ÿ Apple Developer Documentation

Collection View(UICollectionView) ์ปฌ๋ ‰์…˜๋ทฐ

๋น„๋น„ bibi 2022. 4. 19. 11:49

Collection View(UICollectionView) ์ปฌ๋ ‰์…˜๋ทฐ

https://developer.apple.com/library/archive/documentation/WindowsViews/Conceptual/CollectionViewPGforIOS/Introduction/Introduction.html#//apple_ref/doc/uid/TP40012334-CH1-SW1

https://www.raywenderlich.com/18895088-uicollectionview-tutorial-getting-started

์ปฌ๋ ‰์…˜๋ทฐ๋ž€ ์•„์ดํฐ์˜ ์‚ฌ์ง„ ์•ฑ์ฒ˜๋Ÿผ, ์—ฌ๋Ÿฌ ํ•ญ๋ชฉ์„ ๋‚˜์—ดํ•˜๋Š” ๋ ˆ์ด์•„์›ƒ์ด๋‹ค.

์ปฌ๋ ‰์…˜๋ทฐ์— ์ค‘์š”ํ•œ ๊ฒƒ์€ ๋ทฐ๋“ค์„ ๊ฐ€์ ธ์™€์„œ ๋ฐฐ์น˜ํ•˜๋Š” ๊ฒƒ์ผ ๋ฟ, ๊ทธ ๋‚ด์šฉ์€ ์ค‘์š”ํ•˜์ง€ ์•Š๋‹ค.

Understanding the interactions between the collection view, its data source, the layout object, and your custom objects is crucial for using collection views in your app, particularly in smart and efficient ways.

์ปฌ๋ ‰์…˜๋ทฐ, ๋ฐ์ดํ„ฐ์†Œ์Šค, ๋ ˆ์ด์•„์›ƒ ๊ฐ์ฒด, ์ปค์Šคํ…€ ๊ฐ์ฒด ๊ฐ„์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ์ดํ•ดํ•˜๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค.

  • ๋‹ค๋ฅธ ๋ชจ๋“  ๋ทฐ๋“ค๊ณผ ๊ฐ™์ด ์…€๊ณผ ๋ ˆ์ด์•„์›ƒ ์กฐ์ •์— ์ œ์Šค์ฒ˜ ์ธ์‹๊ธฐ(Gesture Recognizer)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Custom Layout๋ฅผ ๋งŒ๋“ค์–ด ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๋„ ๊ฐ€๋Šฅํ•˜๋‹ค.

์ปฌ๋ ‰์…˜๋ทฐ๋Š” ํ…Œ์ด๋ธ”๋ทฐ์— ๋น„ํ•ด...

ํ…Œ์ด๋ธ”๋ทฐ์™€ ๋น„๊ตํ–ˆ์„ ๋•Œ ๋ฐ์ดํ„ฐ ๋‚˜์—ด์ด ๋” ์ž์œ ๋กญ๋‹ค.

ํ…Œ์ด๋ธ”๋ทฐ์™€ ๊ฐ™์ด Datasource & Delegate ํ”„๋กœํ† ์ฝœ์„ ์ด์šฉํ•˜๋Š” ํŒจํ„ด์„ ์‚ฌ์šฉํ•œ๋‹ค.

UICollectionView์˜ ๊ตฌ์„ฑ์š”์†Œ

  • UICollectionView : ์ปจํ…์ธ ๊ฐ€ ๋ฐฐ์น˜๋˜๋Š” ๋ฉ”์ธ ๋ทฐ. UITableView์™€ ๋น„์Šทํ•˜๊ฒŒ ์ƒ๊ฒผ๋‹ค. ํ…Œ์ด๋ธ”๋ทฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ UIScrollView์˜ ํ•˜์œ„ ํด๋ž˜์Šค์ด๋‹ค.
  • UICollectionViewCell : UITableViewCell๊ณผ ๋น„์Šทํ•˜๋‹ค. ๋ทฐ์˜ ์ปจํ…์ธ ๋ฅผ ๊ตฌ์„ฑํ•˜๋ฉฐ ์ปฌ๋ ‰์…˜ ๋ทฐ์˜ ํ•˜์œ„ ๋ทฐ์ด๋‹ค. cell์€ ์ฝ”๋“œ ๋˜๋Š” InterfaceBuilder๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.
  • Supplementary Views : ์ปฌ๋ ‰์…˜ ๋ทฐ ์•ˆ์— ์žˆ์–ด์•ผ ํ•˜์ง€๋งŒ ์…€ ์•ˆ์— ๋„ฃ์„ ์ˆ˜ ์—†๋Š” ๋ถ€๊ฐ€ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค„ ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค. ๊ฐœ๋ฐœ์ž๋“ค์€ ๋ณดํ†ต supplementary view๋ฅผ header๋‚˜ footer์— ์‚ฌ์šฉํ•œ๋‹ค.

์ปฌ๋ ‰์…˜๋ทฐ๋„ DecorationViews๋ฅผ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. DecorationView๋Š” ์ •๋ณด๋ฅผ ๋‹ด์ง„ ์•Š์ง€๋งŒ ์ปฌ๋ ‰์…˜๋ทฐ์˜ ์™ธ์–‘์„ ์ข‹๊ฒŒ ํ•˜๊ธฐ ์œ„ํ•ด ๋ทฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉํ•œ๋‹ค(๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€, ์žฅ์‹๋ฌผ ๋“ฑ). DecorationView๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ปค์Šคํ…€ ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค.

UICollectionViewLayout ์‚ฌ์šฉํ•˜๊ธฐ

์œ„์˜ ๊ตฌ์„ฑ์š”์†Œ ์™ธ์—, ์ปฌ๋ ‰์…˜๋ทฐ๋Š” ๋ ˆ์ด์•„์›ƒ ๊ฐ์ฒด๋ฅผ ๊ฐ€์ง„๋‹ค. ๋ ˆ์ด์•„์›ƒ ๊ฐ์ฒด๋Š” ์ปจํ…์ธ ์˜ ํฌ๊ธฐ, ์œ„์น˜ ๋“ฑ์˜ ์†์„ฑ์„ ๋‹ด๋‹นํ•œ๋‹ค.

๋ ˆ์ด์•„์›ƒ ๊ฐ์ฒด๋Š” UICollectionViewLayout์˜ ํ•˜์œ„ ํด๋ž˜์Šค์ด๋‹ค. ์‹คํ–‰ ์ค‘์— ๋ ˆ์ด์•„์›ƒ์„ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. ์‹ฌ์ง€์–ด ์ž๋™์œผ๋กœ ํ•œ ๋ ˆ์ด์•„์›ƒ์—์„œ ๋‹ค๋ฅธ ๋ ˆ์ด์•„์›ƒ์œผ๋กœ ๋ฐ”๊พธ๋Š” ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ๋„ฃ์„ ์ˆ˜๋„ ์žˆ๋‹ค.

์ปค์Šคํ…€ ๋ ˆ์ด์•„์›ƒ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด UICollectionVieweLayout์„ ์ƒ์†ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์• ํ”Œ์ด flow๊ธฐ๋ฐ˜์˜ ๊ธฐ๋ณธ ๋ ˆ์ด์•„์›ƒ์ธ UICollectionViewFlowLayout์„ ์ œ๊ณตํ•œ๋‹ค. ์š”์†Œ(element)๋Š” grid view์™€ ๊ฐ™์ด ํฌ๊ธฐ์— ๋”ฐ๋ผ ์ฐจ๋ก€๋Œ€๋กœ ๋ฐฐ์น˜๋œ๋‹ค. ํฅ๋ฏธ๋กœ์šด ๋™์ž‘๊ณผ ์‹œ๊ฐ์  ํšจ๊ณผ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์ด ๋ ˆ์ด์•„์›ƒ ํด๋ž˜์Šค(๋˜๋Š” ๊ทธ ํ•˜์œ„ ํด๋ž˜์Šค)๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


  • ์ฝ”๋“œ ๊ธฐ๋ฐ˜์œผ๋กœ UICollectionView ๊ตฌํ˜„ํ•˜๊ธฐ
  • UICollectionViewCell ๋ฐฐ์น˜ํ•˜๊ณ  ๋žœ๋ค ์ƒ‰์ƒ ์ง€์ •ํ•˜๊ธฐ