IOS CollectionView Properties and Methods Daquan

Source: Internet
Author: User

To tidy up a bit collectionview:

1.UICollectionViewLayout

Uicollectionviewlayout can be said to be Uicollectionview's brain and center, which is responsible for organizing each cell, supplementary view and decoration views to set their own properties, layout determines how the Uicollectionview is displayed on the interface. Before the presentation, it is generally necessary to generate an appropriate Uicollectionviewlayout subclass object and assign it to the CollectionView Collectionviewlayout property

Uicollectionviewflowlayout *collectionviewflowlayout=[[uicollectionviewflowlayout Alloc]init];

Minimum spacing

collectionviewflowlayout.minimuminteritemspacing=0.0;

Minimum line spacing

collectionviewflowlayout.minimumlinespacing=0.0;

Scrolling direction

Collectionviewflowlayout.scrolldirection = uicollectionviewscrolldirectionvertical;

Insert Section

Collectionviewflowlayout.sectioninset = Uiedgeinsetsmake (0.0, 0.0, 0, 0.0);

It 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

Collectionviewflowlayout.itemsize=cgsizemake (120, 120);

Collectionviewflowlayout.estimateditemsize=cgsizemake (120, 120);

The direction of the scroll view is determined by the attribute scrolldirection, which affects the basic direction of flow layout and the width between the sections determined by the header and the footer.

collectionviewflowlayout.scrolldirection=uicollectionviewscrolldirectionvertical;

Header and footer dimensions are equally divided into global and partial. Note that depending on the scrolling direction, only one of the height and width of the header and footer will work. The width of the section is the height of the dimension when scrolling vertically, and the width works when scrolling horizontally

Collectionviewflowlayout.headerreferencesize=cgsizemake (100, 40);

Collectionviewflowlayout.footerreferencesize=cgsizemake (100, 40);

Properties of 2.UICollectionView

Collectionview.backgroundcolor=[uicolor Whitecolor];

collectionview.delegate=self;

collectionview.datasource=self;

Allow selection

Collectionview.allowsselection=yes;

Allow multiple selections

Collectionview.allowsmultipleselection=yes;

Register related classes

[CollectionView Registerclass:[ccell class] forcellwithreuseidentifier:@ "cell"];

[CollectionView Registerclass:[uicollectionreusableview class] Forsupplementaryviewofkind: Uicollectionelementkindsectionheader withreuseidentifier:@ "Header"];

[CollectionView Registerclass:[uicollectionreusableview class] Forsupplementaryviewofkind: Uicollectionelementkindsectionfooter withreuseidentifier:@ "Footer"];

You can also use NIB

[[CollectionView registernib:<# (uinib *) #> forcellwithreuseidentifier:<# (NSString *) #>]

CollectionView registernib:<# (uinib *) #> forsupplementaryviewofkind:<# (NSString *) #> withreuseidentifier:<# (NSString *) #>

Method of 3.UICollectionView

Uicollectionviewdatasource Agent

Number of cells per section

-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section

{

if (section==0) {

return 6;

}

Else

{

return 4;

}

}

Number of knots

-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView

{

return 2;

}

Cell

-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath

{

Ccell *cell =[collectionview dequeuereusablecellwithreuseidentifier:@ "cell" forindexpath:indexpath];

Cell.backgroundcolor = [Uicolor colorwithred:arc4random ()%256/255.0 green:arc4random ()%256/255.0 blue:arc4random ()% 256/255.0 Alpha:1];

Cell.myimageview=[[uiimageview alloc]initwithimage:[uiimage imagenamed:@ "Image0.jpg"];

Cell.namelabel=[[uilabel Alloc]init];

Cell.namelabel.text=[nsstring stringwithformat:@ "%ld%ld", Indexpath.section,indexpath.row];

return cell;

}

End of Section Head

-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) Indexpath

{

if ([Kind Isequaltostring:uicollectionelementkindsectionfooter]) {

Uicollectionreusableview *footer=[collectionview dequeuereusablesupplementaryviewofkind:kind WithReuseIdentifier: @ "Footer" Forindexpath:indexpath];

Footer.backgroundcolor=[uicolor Yellowcolor];

UILabel *label=[[uilabel alloc]initwithframe:cgrectmake (0, 0, 100, 40)];

[Email protected] "label";

Label.textalignment=nstextalignmentcenter;

[Footer Addsubview:label];

return footer;

}

Else

{

Uicollectionreusableview *header=[collectionview dequeuereusablesupplementaryviewofkind:kind WithReuseIdentifier: @ "Header" Forindexpath:indexpath];

Header.backgroundcolor=[uicolor Bluecolor];

return Header;

}

}

Uicollectionviewdelegate Agent

Whether to highlight when selected

-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldhighlightitematindexpath: (Nsindexpath *) IndexPath

{

if (indexpath.row%2==0) {

return YES;

}

return NO;

}

When highlight is selected

-(void) CollectionView: (Uicollectionview *) CollectionView Didhighlightitematindexpath: (Nsindexpath *) IndexPath

{

NSLog (@ "Didhighlightitematindexpath");

}

Allow to be selected

-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: (Nsindexpath *) IndexPath

{

return YES;

}

Allow to be canceled

-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shoulddeselectitematindexpath: (Nsindexpath *) IndexPath

{

return YES;

}

Select a cell

-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath

{

NSLog (@ "Didselectitematindexpath section=%ld row=%ld", Indexpath.section,indexpath.row);

}

Cancel a cell

-(void) CollectionView: (Uicollectionview *) CollectionView Diddeselectitematindexpath: (Nsindexpath *) IndexPath

{

NSLog (@ "Diddeselectitematindexpath section=%ld row=%ld", Indexpath.section,indexpath.row);

}

Highlight does not display

-(void) CollectionView: (Uicollectionview *) CollectionView Didunhighlightitematindexpath: (Nsindexpath *) IndexPath

{

NSLog (@ "Didunhighlightitematindexpath");

}

About to show Uicollectionviewcell

-(void) CollectionView: (Uicollectionview *) CollectionView Willdisplaycell: (Uicollectionviewcell *) cell Foritematindexpath: (Nsindexpath *) Indexpath

{

NSLog (@ "Willdisplaycell");

}

Footer and headers for section will be displayed

-(void) CollectionView: (Uicollectionview *) CollectionView Willdisplaysupplementaryview: (Uicollectionreusableview * ) View Forelementkind: (NSString *) elementkind Atindexpath: (Nsindexpath *) Indexpath

{

NSLog (@ "Willdisplaysupplementaryview");

}

Uicollectionviewcell Display Complete

-(void) CollectionView: (Uicollectionview *) CollectionView Didenddisplayingcell: (Uicollectionviewcell *) cell Foritematindexpath: (Nsindexpath *) Indexpath

{

NSLog (@ "Didenddisplayingcell");

}

Show section's footer and header complete

-(void) CollectionView: (Uicollectionview *) CollectionView Didenddisplayingsupplementaryview: ( Uicollectionreusableview *) View Forelementofkind: (NSString *) elementkind Atindexpath: (Nsindexpath *) IndexPath

{

NSLog (@ "Didenddisplayingsupplementaryview");

}

Whether to show copy, cut, paste, etc.

-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldshowmenuforitematindexpath: (NSIndexPath *) Indexpath

{

return YES;

}

Whether to show the Action accessibility feature

-(BOOL) CollectionView: (Uicollectionview *) CollectionView canperformaction: (SEL) Action Foritematindexpath: ( Nsindexpath *) Indexpath Withsender: (ID) sender

{

SEL [email protected] (copy:);

if (sel==action) {

return YES;

}

return NO;

}

Determine what action is selected

-(void) CollectionView: (Uicollectionview *) CollectionView performaction: (SEL) Action Foritematindexpath: ( Nsindexpath *) Indexpath Withsender: (ID) sender

{

SEL [email protected] (copy:);

if (sel==action) {

NSLog (@ "AAAAA");

}

}

IOS CollectionView Properties and Methods Daquan

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.