Introduction to the

Supplementary ViewsAppend views (similarHeaderorFooter)Decoration ViewsDecorative view (used as background display)Copy the code

As for the style and organization of cell, collectionView is much more complex than tableView, so it is not defined in a style similar to tableView. Instead, a class is specially used to describe the layout and behavior of collectionView. That’s UICollectionViewLayout.

And we mainly talk about UICollectionViewLayout, because it is not only the difference between collectionView and tableView, but also the essence of the whole UICollectionView.

UICollectionViewLayoutAttributes

@property (nonatomic) CGRect frame
@property (nonatomic) CGPoint center
@property (nonatomic) CGSize size
@property (nonatomic) CATransform3D transform3D
@property (nonatomic) CGFloat alpha
@property (nonatomic) NSInteger zIndex
@property (nonatomic, getter=isHidden) BOOL hidden
Copy the code

As you can see, UICollectionViewLayoutAttributes instance includes such as borders, center, size, shape, transparency, hierarchical relationships, and whether the hidden information.

  1. A cell corresponds to a UICollectionViewLayoutAttributes object
  2. UICollectionViewLayoutAttributes object determines the decoration of the cell location (frame)

Custom UICollectionViewLayout

UICollectionViewLayout provides layout information to UICollectionView, including not only cell layout information, but also append view and decorate view layout information. A common way to implement a custom Layout is to inherit the UICollectionViewLayout class and override the following methods:

-(void) The prepareLayout method is automatically called to ensure that the layout instance is correct. - (CGSize) collectionViewContentSize return collectionView content size - (NSArray *) layoutAttributesForElementsInRect (CGRect) the rect 1. Returns all the layout attribute of the element (2) of the rect returns include UICollectionViewLayoutAttributes NSArray 3. UICollectionViewLayoutAttributes can be a cell, Additional views or decoration view information, through different UICollectionViewLayoutAttributes initialization method can get different types of UICollectionViewLayoutAttributes:  1)layoutAttributesForCellWithIndexPath: 2)layoutAttributesForSupplementaryViewOfKind:withIndexPath: 3)layoutAttributesForDecorationViewOfKind:withIndexPath: -(UICollectionViewLayoutAttributes )layoutAttributesForItemAtIndexPath:(NSIndexPath )indexPath Returns the position of the corresponding to the indexPath cell layout properties - UICollectionViewLayoutAttributes layoutAttributesForSupplementaryViewOfKind: (nsstrings )kind atIndexPath:(NSIndexPath *)indexPath returns the layout property of the append view corresponding to the position of the indexPath, If there is no additional views don't overload - layoutAttributesForDecorationViewOfKind: (nsstrings) decorationViewKind UICollectionViewLayoutAttributes (*) AtIndexPath :(NSIndexPath)indexPath returns the layout properties of the decorator view corresponding to the position of the indexPath, If there is no decoration view don't overload - (BOOL) shouldInvalidateLayoutForBoundsChange: (CGRect) newBounds when boundary changes, whether should refresh layout. If YES, the required layout information will be recalculated when the boundary changes (typically scroll to somewhere else). Call to orderCopy the code

Call to order

1) -(void)prepareLayout A. Collection View will only call '-PrepareLayout' once on the first layout, A message notifying a Layout instance object for the first time. B. Collection View will call C again after the Layout object is invalidated and before requerying. Any method that inherits from UICollectionViewLayout needs to override this method, Are usually ready within this method ` layoutAttributesForElements (in) ` the method to use to ` UICollectionViewLayoutAttributes ` array. 2) -(CGSize) collectionViewContentSize a. B. This method is called every time the Collection View is moved. And this method is called several times 3) - (NSArray *) layoutAttributesForElementsInRect (CGRect) the rect The appearance of the initial layout will be decided by the method returns UICollectionViewLayoutAttributes. 1) -invalidatelayOut is sent to the current layout when the layout needs to be updated. This message will be returned immediately and the next loop is scheduled to refresh the current layout. 2)-prepareLayout, 3) in order to call - collectionViewContentSize and - layoutAttributesForElementsInRect to generate an updated layout.Copy the code