[iOS] CollectionView의 이해

신용철·2020년 9월 18일
0

1. CollectionView의 일반적인 특징

  • CollectionView는 UIScrollView를 상속받습니다
  • CollectionView는 header와 footer를 갖지만 TableView처럼 headerView를 직접 호출하지않고 Cell의 subview 혹은 supplementary View를 사용합니다.
  • supplementaryView 는 UICollectionViewCell의 super class인 UICollectionReusableView입니다.
  • supplementaryView는 "kind"와 관련이 있습니다.
  • 우리가 생각하는 collectionView header와 footer는 "kind"입니다.
  • supplementaryView는 재사용이 가능합니다
  • collectionView(:viewForSupplementaryElementOfKind: at:)과 dequeueReusableSupplementraryView(ofKind:withReusableIdentifier:for:)메서드를 이용하여 구현합니다.
  • collectionView는 sectionIndex가 없습니다
  • collectionView와 cell은 편집모드가 없습니다.

2. TableView와 CollectionView의 layout 구성의 차이

  • TableView는 cell의 width 값이 talbleView.frame.width이며 cell과 cell이 붙어있습니다. 그리고 cell의 height는 tableView 또는 delegate가 설정할 수 있습니다.
  • 하지만 CollectionView는 이러한 규칙도 없고 사실 아무것도 안합니다. 모든 것을 collectionView Layout에게 일임합니다.
  • collectionViewLayout은 UICollectionViewLayout의 하위 class 입니다.

3. CollectionView구성에 필요한 필수 정보

  • contentSize: collectionView는 ScrollView이기 때문에 스크롤해야하는 contents의 전체 크기를 알아야 합니다.
  • layoutAttributesForElements(in:): content들을 collectionvView 내부 어디에 그릴지에 대한 정보가 필요합니다. 이 속성은 그려질 contents들의 위치정보 묶음이라고 생각하면 됩니다.
  • layoutAttributesForElements에서 위치정보를 구하기 위해 CollectionView에게 collectionView의 크기, section의 갯수, cell의 갯수를 요청하고 CollectionView는 이를 DataSource에 다시 요청해서 답을 줍니다.
  • collectionView layout은 위 정보들을 기반으로 cell의 위치정보를 CollectionView에게 알려주고 collectionView는 cell을 그리게 됩니다.
  • Collection layout을 사용하기 위해서는 UICollectionViewLayout 혹은 UICollectionViewCompositionalLayout(iOS 13이상)을 사용해야 합니다.
  • CollectionViewLayout을 인스턴스화 하면 scroll 방향, cell의 크기, 간격, inset정보와 header와 footer를 설정할 수 있습니다.
profile
iOS developer

0개의 댓글