1. Compliance with agreements
<UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
2. Create
Uicollectionviewflowlayout * layout = [[Uicollectionviewflowlayout alloc]init];
layout.minimuminteritemspacing = 10; The spacing between minimum item
layout.minimumlinespacing = 10;//Minimum line spacing
CollectionView = [[Uicollectionview alloc]initwithframe:cgrectmake (0, 0, WIDTH, height-64-49) Collectionviewlayout: Layout];
Collectionview.delegate = self;
Collectionview.datasource = self;
Important
1>, if it is a code-defined cell, it should be registered with the following method
[CollectionView Registerclass:[piccollectionviewcell class] forcellwithreuseidentifier:@ "CC"];
2>, if the cell defined by Xib is to be used
[CollectionView registernib:[uinib nibwithnibname:@ "Piccollectionviewcell" Bundle:[nsbundle MainBundle]] forcellwithreuseidentifier:@ "CC"];
3, return the number of item
-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
{
return datasourse.count;
}
4. Cell multiplexing
Xib and code both use the following methods
Piccollectionviewcell * cell = [CollectionView dequeuereusablecellwithreuseidentifier:@ "CC" ForIndexPath:indexPath];
5. Important Protocol Method
1> returns the size of item, the system automatically sets the number of items displayed per row according to the size of item (Layout.size method can be used)
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout *) collectionviewlayout Sizeforitematindexpath: (Nsindexpath *) Indexpath
{
Cgsize size = Cgsizemake (90, 80);
return size;
}
2>//Returns whether this Uicollectionview can be selected
-(BOOL) CollectionView: (Uicollectionview *) CollectionView Shouldselectitematindexpath: (Nsindexpath *) IndexPath
{ return YES;
}
How to use Uicollectionview