var delegate: UICollectionViewDelegate?
- collection view의 delgate object.
- 컬렉션 요소와 사용자간 상호작용을 처리한다.
- UICollectionViewDelegate: collection view의 동작을 관리하는 메소드들의 protocol이다.
- 모든 메소드들은 optional이다.
- 공통적으로 delegate에게 알려주는 것은 Void / delegate이 return해 주는 것은 non-Void라고 생각하자.
func collectionView(UICollectionView, shouldSelectItemsAt: Set<IndexPath>) -> Set<IndexPath>
func collectionView(UICollectionView, didSelectItemsAt: Set<IndexPath>)
func collectionView(UICollectionView, shouldDeselectItemsAt: Set<IndexPath>) -> Set<IndexPath>
func collectionView(UICollectionView, didDeselectItemsAt: Set<IndexPath>)
func collectionView(UICollectionView, shouldBeginMultipleSelectionInteractionAt: IndexPath) -> Bool
func collectionView(UICollectionView, didBeginMultipleSelectionInteractionAt: IndexPath)
func collectionViewDidEndMultipleSelectionInteraction(UICollectionView)
func collectionView(UICollectionView, shouldHighlightItemAt: IndexPath) -> Bool
func collectionView(UICollectionView, didHighlightItemAt: IndexPath)
func collectionView(UICollectionView, didUnhighlightItemAt: IndexPath)
func collectionView(UICollectionView, willDisplay: UICollectionViewCell, forItemAt: IndexPath)
func collectionView(UICollectionView, willDisplaySupplementaryView: UICollectionReusableView, forElementKind: String, at: IndexPath)
func collectionView(UICollectionView, didEndDisplaying: UICollectionViewCell, forItemAt: IndexPath)
func collectionView(UICollectionView, didEndDisplayingSupplementaryView: UICollectionReusableView, forElementOfKind: String, at: IndexPath)
func collectionView(UICollectionView, transitionLayoutForOldLayout: UICollectionViewLayout, newLayout: UICollectionViewLayout) -> UICollectionViewTransitionLayout
func collectionView(UICollectionView, targetContentOffsetForProposedContentOffset: CGPoint) -> CGPoint
func collectionView(UICollectionView, targetIndexPathForMoveFromItemAt: IndexPath, toProposedIndexPath: IndexPath) -> IndexPath
func collectionView(UICollectionView, canFocusItemAt: IndexPath) -> Bool
func indexPathForPreferredFocusedView(in: UICollectionView) -> IndexPath?
func collectionView(UICollectionView, shouldUpdateFocusIn: UICollectionViewFocusUpdateContext) -> Bool
func collectionView(UICollectionView, didUpdateFocusIn: UICollectionViewFocusUpdateContext, with: UIFocusAnimationCoordinator)
var collectionViewLayout: UICollectionViewLayout { get set }
- 이 속성에 새로운 layout객체를 할당해 새로운 layout을 collection view의 item에 적용시킬 수 있다.
- layout 객체는 UICollectionViewLayout class를 상속해야한다.
func setCollectionViewLayout(_ layout: UICollectionViewLayout, animated: Bool)
func setCollectionViewLayout(_ layout: UICollectionViewLayout, animated: Bool, completion: ((Bool) -> Void)? = nil)
func startInteractiveTransition(to: UICollectionViewLayout, completion: UICollectionView.LayoutInteractiveTransitionCompletion?) -> UICollectionViewTransitionLayout
func finishInteractiveTransition()
func cancelInteractiveTransition()
class UICollectionViewFlowLayout: UICollectionViewLayout
layout을 지정해주지 않았을 때는 UICollectionViewFlowLayout을 default로 가진다.
만약 default인 flow layout을 layout object로 가진다고 했을때,
protocol UICollectionViewDelegateFlowLayout
프로토콜의 메소드들이 아이템(cell, supplement view 등)의 크기나 아이템 간의 공간 등을 정의해준다.
예시로, 다음과 같은 메소드들이 있다.
- func collectionView(UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize
: item cell의 크기를 정의하는 메소드
- func collectionView(UICollectionView, layout: UICollectionViewLayout, insetForSectionAt: Int) -> UIEdgeInsets
: 특정 section의 view들의 margin을 정의하는 메소드 (cell간의 간격 / footer, header 간의 간격 등)
Flow Layout이 어떻게 생기는가?
1. layout객체 작성
2. cell width/height 결정 (필수!)
- cell 간격 결정 (필요할 경우)
- header/footer 사이즈, 간격 결정(필요할 경우)
3. layout 스크롤 방향 결정
* item하나의 크기를 결정하는 방법.
1. (모든 item의 크기가 같은 경우) Layout Object의 itemSize 속성을 지정한다.
2. (item마다 크기가 다른경우) collectionView(_:layout:sizeForItemAt:) method를 호출한다.
3. estimatedItemSize 속성을 활용한다. (iOS 8~ 비추!)
class UICollectionViewTransitionLayout: UICollectionViewLayout
UICollectionViewLayout을 Custom해서 (상속받아서)새로운 커스텀 layout class를 만드는 방법 두 가지
1. storyboard에서 collection view의 layout class를 변경한다.
2. collectionView.collectionViewLayout = MyCustomLayout()
코드로 할당해준다.
UICollectionViewLayout의 Subclass는 아래 설명의 메소드들을 구현해야한다.
func prepare()
: Layout작업이 수행되려고 할 때마다 이 method가 호출된다. collection view의 크기, item 위치 등을 결정하기 위해 필요한 선 연산을 수행한다.var collectionViewContentSize: CGSize { get }
: prepare에서 수행한 연산을 기반으로 collection view 컨텐츠의 전체 크기를 리턴한다.func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]?
: 특정 사각형 범위의 모든 cell과 view의 layout atrribute를 리턴한다. (이 사각형 범위는 보여지는 화면의 크기와 같을 수도 있지만 아닐 수도 있다.)layoutAttributesForItem(at: indexPath)
: 특정 indexPath의 모든 cell과 view의 layout atrribute를 리턴한다.layoutAttributesForSupplementaryView(ofKind:at:)
(supplementary view 사용 시)layoutAttributesForDecorationView(ofKind:at:)
(decoration view 사용 시)shouldInvalidateLayout(forBoundsChange:)
: bound가 바뀌어서 layout이 update가 필요한지 리턴한다.invalidLayout()
: 명시적으로 layout을 update하라고 요청한다.func reloadData()
func reloadSections(IndexSet)
func reloadItems(at: [IndexPath])
func insertItems(at: [IndexPath])
func moveItem(at: IndexPath, to: IndexPath)
func deleteItems(at: [IndexPath])
func insertSections(IndexSet)
func moveSection(Int, toSection: Int)
func deleteSections(IndexSet)
func performBatchUpdates((() -> Void)?, completion: ((Bool) -> Void)?)