IOS---Uicollectionview understanding and common API translations

Source: Internet
Author: User

IOS---Uicollectionview understanding and common API translation Uicollectionview
    • 1. Layout parameters must be set
    • 2. Register the Cell
Usage is similar to the UITableView class. Automatic reuse must be registered for initialization. Using Uicollectionview must implement UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayoutThese three agreements. The composition of the Collection view, we can see there are three parts:
    • Cells
    • Supplementary views Append view (like header or footer)
    • Decoration views Decorative view (used as a background display)
First, Uicollectionviewlayout
    • The essence of Uicollectionview
    • Layout determines how the Uicollectionview is displayed on the interface. Before presenting, it is generally necessary to generate an appropriate Uicollectionviewlayout subclass object and assign it to the Collectionviewlayout property of CollectionView.
1.UICollectionViewFlowLayout
    • The simplest and most commonly used default layout object,??? Uicollectionviewflowlayout. Flow layout is simply a line-aligned layout,
Common Properties
The line spacing can also be collectionView:layout:minimumLineSpacingForSectionAtIndex by:@property (nonatomic)CGFloat minimumlinespacing;Set the spacing between cells@property (nonatomic)CGFloat minimuminteritemspacing;Defines the size of each item. By setting itemsize, you can change the size of all cells globally, and if you want to size a cell,You can use the-collectionview:layout:sizeforitematindexpath: method.@property (nonatomic)Cgsize itemsize;@property (nonatomic)Cgsize estimateditemsizeScroll direction, default is horizontal//uicollectionviewscrolldirectionvertical// Uicollectionviewscrolldirectionhorizontal @property ( nonatomic) uicollectionviewscrolldirection scrolldirection; Depending on the scrolling direction, only one of the height and width of the header and footer will work. //vertical scrolling when the width of the section is the height of the dimension, while horizontal scrolling is the width of the function,  @property (nonatomic) cgsize headerreferencesize; @property (nonatomic) cgsize Footerreferencesize;//Group spacing indent  @property (nonatomic) uiedgeinsets sectioninset;           
Uicollectionviewdelegateflowlayout Proxy method
Sets the size of the specified cell-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath;Sets the margin for the CollectionView (specified area)-(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Insetforsectionatindex: (nsinteger) section; //sets the minimum line spacing for the cell within the specified area, or you can set the Uicollectionviewflowlayout property of a setting directly-(minimumlinespacing class= "hljs-built_in" >cgfloat) CollectionView: (uicollectionview *) CollectionView Layout: (uicollectionviewlayout *) collectionviewlayout Minimumlinespacingforsectionatindex: (nsinteger) section/ /Set the minimum spacing of cells within the specified area, or you can set the Minimuminteritemspacing property of the Uicollectionviewflowlayout directly-(uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) section            
2.UICollectionViewLayoutAttributes
    • A very important class,
Property List
Border@property (nonatomic)CGRect FrameCenter Point@property (nonatomic)cgpoint center//size  @property ( Span class= "Hljs-keyword" >nonatomic) cgsize size//shape  @property (nonatomic) CATransform3D Transform3d//transparency  @property ( nonatomic) cgfloat alpha//Hierarchical relationship  @property (nonatomic) nsinteger ZIndex //whether to hide  @property (nonatomic, Getter=ishidden) bool hidden         
3. Custom Uicollectionviewlayout
    • The Uicollectionviewlayout feature provides layout information to the Uicollectionview.
    • Inherits the Uicollectionviewlayout class.
overriding method
Returns the size of the contents of the CollectionView-(Cgsize) collectionviewcontentsizeReturns the layout properties of all elements in a rect/* Returns the information that contains uicollectionviewlayoutattributes nsarrayuicollectionviewlayoutattributes can be cell, append view or adornment view, Different types of uicollectionviewlayoutattributes:*/-can be obtained by different uicollectionviewlayoutattributes initialization methods (Nsarray *) Layoutattributesforelementsinrect: (CGRect) rectReturns the Layout property of the cell that corresponds to the location of the Indexpath-(Uicollectionviewlayoutattributes _) Layoutattributesforitematindexpath: (nsindexpath _) Indexpath//returns the layout properties of the appended view corresponding to the location of Indexpath. If no append view is not overloaded-(uicollectionviewlayoutattributes _) Layoutattributesforsupplementaryviewofkind: (nsstring _) Kind AtIndexPath: (nsindexpath *) Indexpath//returns the layout properties of the decorated view corresponding to the Indexpath location, if no adornment view is not overloaded-( Span class= "hljs-built_in" >uicollectionviewlayoutattributes *) Layoutattributesfordecorationviewofkind: (nsstring_) decorationviewkind Atindexpath: (NSIndexPath _) Indexpath//if the layout should be refreshed when the boundary changes. If yes, the required layout information is recalculated when the boundary changes (typically scroll to somewhere else). -(bool) Shouldinvalidatelayoutforboundschange: (CGRect) Newbounds            
    • It is also necessary to understand that after initializing a uicollectionviewlayout instance, a series of preparation methods are automatically called to ensure that the layout instance is correct.

    • First,-(void) preparelayout will be called,
      • By default, the method does not do anything, but in its own sub-class implementation
      • , we usually set some necessary layout structure and initial required parameters in this method.
    • After that-(cgsize) collectionviewcontentsize will be called,
      • To determine the size that the collection should occupy. Note that the dimensions here do not refer to the dimensions of the viewable part, but to the size of all the contents.
      • The essence of CollectionView is a scrollview, so this size is needed to configure scrolling behavior.
Second, the number of uicollectionview1.uicollectionviewdatasource①section? How many item are there in a section of-numberofsectionsincollection:②? What cell Collectionview:numberofitemsinsection:③ should be displayed for a location?-collectionview:cellforitematindexpath:2. Uicollectionviewdelegate
Triggered when the item specified at Indexpath is selected-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexpathReturns whether this Uicollectionview can be selected-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: (Nsindexpath *) Indexpath;//below are three and highlighted methods:-(bool) CollectionView: (uicollectionview *) CollectionView Shouldhighlightitematindexpath: ( Nsindexpath *) indexpath-(void) CollectionView: ( Uicollectionview *) CollectionView Didhighlightitematindexpath: (NSIndexPath *) indexpath-(void) CollectionView: (UICollectionView *) CollectionView Didunhighlightitematindexpath: (nsindexpath *) IndexPath/* event is processed in the following order: 1. Press 2.shouldHighlightItemAtIndexPath (if return yes then execute down, otherwise execute until here) 3. Didhighlightitematindexpath (highlight) 4. Finger Release 5.didUnhighlightItemAtIndexPath (cancel highlight) 6.shouldSelectItemAtIndexPath ( If you return yes then execute down, otherwise execute until here) 7.didSelectItemAtIndexPath (perform selection event)        

IOS---Uicollectionview and common API translations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.