First, meet Uicollectionview.
[OBJC]View Plaincopy
- Ns_class_available_ios (6_0) @interface uicollectionview:uiscrollview
The Uicollectionview and Uicollectionviewcontroller classes are IOS6 's newly introduced APIs for presenting collection views, with a more flexible layout that enables multi-column layouts, similar to UITableView and Uitableviewcontroller class.
Using Uicollectionview must implement Uicollectionviewdatasource,uicollectionviewdelegate, Uicollectionviewdelegateflowlayout these three agreements.
Here are some common methods, specific use can refer to the demo: point I download Apple official demo: click I download
[OBJC]View Plaincopy
- -(void) Viewdidload
- {
- [Super Viewdidload];
- self. title = @ "Uicollectionview learning";
- //Generate cell via NIB, and then register the nib's view to inherit Uicollectionviewcell
- [self. CollectionView registernib:[uinib nibwithnibname:@ "Sqcollectioncell" bundle: Nil] Forcellwithreuseidentifier:kcellidentifier];
- //Register Headerview NIB's view needs to inherit Uicollectionreusableview
- [self. CollectionView registernib:[uinib nibwithnibname:@ "Sqsupplementaryview" bundle: Nil] Forsupplementaryviewofkind:uicollectionelementkindsectionheader withreuseidentifier: Kheaderidentifier];
- //Register Footerview NIB's view needs to inherit Uicollectionreusableview
- [self. CollectionView registernib:[uinib nibwithnibname:@ "Sqsupplementaryview" bundle: Nil] forsupplementaryviewofkind:uicollectionelementkindsectionfooter withreuseidentifier: Kfooteridentifier];
- //
- self. CollectionView. allowsmultipleselection = YES; The default is no, whether you can select multiple
- }
- -(void) didreceivememorywarning
- {
- [Super didreceivememorywarning];
- //Dispose of any resources, can be recreated.
- }
- #pragma mark-collectionview DataSource
- Section
- -(Nsinteger) Numberofsectionsincollectionview: (uicollectionview *) CollectionView
- {
- return 2;
- }
- Item Count
- -(Nsinteger) CollectionView: (uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section
- {
- return 6;
- }
- The cell is returned must was retrieved from a call To-dequeuereusablecellwithreuseidentifier:forindexpath:
- -(Uicollectionviewcell *) CollectionView: (uicollectionview *) CollectionView Cellforitematindexpath: ( Nsindexpath *) indexpath
- {
- //Reuse cell
- Uicollectionviewcell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:kcellidentifier Forindexpath:indexpath];
- //Assigned value
- Uiimageview *imageview = (Uiimageview *) [cell Viewwithtag:1];
- UILabel *label = (UILabel *) [cell Viewwithtag:2];
- nsstring *imagename = [NSString stringwithformat:@ "%ld. JPG ", (long) Indexpath. row];
- ImageView. image = [UIImage imagenamed:imagename];
- Label. Text = ImageName;
- Cell. backgroundcolor = [Uicolor redcolor];
- return cell;
- }
- The view that is returned must was retrieved from a call To-dequeuereusablesupplementaryviewofkind:withreuseidentifier: Forindexpath:
- -(Uicollectionreusableview *) CollectionView: (uicollectionview *) CollectionView Viewforsupplementaryelementofkind: (nsstring *) Kind atindexpath: (nsindexpath *) indexpath{
- NSString *reuseidentifier;
- if ([Kind isequaltostring:uicollectionelementkindsectionfooter]) {
- Reuseidentifier = Kfooteridentifier;
- }else{
- Reuseidentifier = Kheaderidentifier;
- }
- Uicollectionreusableview *view = [CollectionView dequeuereusablesupplementaryviewofkind:kind Withreuseidentifier:reuseidentifier Forindexpath:indexpath];
- UILabel *label = (UILabel *) [view Viewwithtag:1];
- if ([Kind Isequaltostring:uicollectionelementkindsectionheader]) {
- Label. Text = [NSString stringwithformat:@ "This is header:%d", Indexpath. section];
- }
- Else if ([Kind isequaltostring:uicollectionelementkindsectionfooter]) {
- View. backgroundcolor = [Uicolor lightgraycolor];
- Label. Text = [NSString stringwithformat:@ "This is footer:%d", Indexpath. section];
- }
- return view;
- }
- Define the size of each Uicollectionviewcell
- -(Cgsize) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Sizeforitematindexpath: (nsindexpath *) indexpath
- {
- return Cgsizemake (60, 80);
- }
- Define margin for each section
- -(Uiedgeinsets) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout *) Collectionviewlayout Insetforsectionatindex: (nsinteger) section
- {
- return Uiedgeinsetsmake (15, 15, 5, 15); Top, left, bottom, right, respectively
- }
- Returns the size of the head Headerview
- -(Cgsize) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout *) Collectionviewlayout referencesizeforheaderinsection: (nsinteger) section{
- Cgsize size={320,45};
- return size;
- }
- Returns the size of the head Footerview
- -(Cgsize) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout referencesizeforfooterinsection: (nsinteger) section
- {
- Cgsize size={320,45};
- return size;
- }
- Line spacing between different rows in each section
- -(CGFloat) CollectionView: (uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Minimumlinespacingforsectionatindex: (nsinteger) section
- {
- return 10;
- }
- The spacing between each item
- -(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) Section
- //{
- return 100;
- //}
- A cell was selected
- -(void) CollectionView: (uicollectionview *) CollectionView didselectitematindexpath: (Nsindexpath *) Indexpath
- {
- Uicollectionviewcell *cell = [CollectionView Cellforitematindexpath:indexpath];
- [Cell Setbackgroundcolor:[uicolor Greencolor]];
- }
- Deselect a cell
- -(void) CollectionView: (uicollectionview *) CollectionView diddeselectitematindexpath: (Nsindexpath *) Indexpath
- {
- Uicollectionviewcell *cell = [CollectionView Cellforitematindexpath:indexpath];
- [Cell Setbackgroundcolor:[uicolor Redcolor]];
- }
As follows: Original link: http://blog.csdn.net/Apple_app/article/details/38867123
IOS Uicollectionview Simple to use