Use of IOS Uicollectionview (created with storyboard and xib)

Source: Internet
Author: User

1. In storyboard, drag out 1 Uicollectionviewcontroller

2. New File--cocoa Touch Class, inherited from Uicollectionviewcontroller, assuming the name is Collectiondemo

3. In storyboard, change the Uicollectionviewcontroller class that you just dragged out to Collectiondemo

4. Implementation in COLLECTIONDEMO.M, data source method

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

{

return self.photos.count;

}

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

{

static NSString *id = @ "Cell";

Customcell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:id Forindexpath:indexpath];

Cell.imageView.image = [UIImage Imagenamed:self.photos[indexpath.item]];

return cell;

}

5. Modify the storyboard Collectionviewcell, such as modify the background color, put a picture, set the constraints of the picture, the most important thing is to modify the reuse tag as a cell, that is, the code is written in accordance with the

6. Create a new custom Collectionviewcell class, add the export, that is, with the storyboard control to do the connection

@interface Customcell:uicollectionviewcell

@property (Weak, nonatomic) Iboutlet Uiimageview *imageview;

@end

7. Operation Effect

8. Compared with pure code creation CollectionView, the following steps are saved

[CollectionView Registerclass:[customcell class] forcellwithreuseidentifier:id]; //Register cell

Uicollectionview *collectionview = [[Uicollectionview alloc] InitWithFrame:self.view.bounds collectionviewlayout:[[ Uicollectionviewflowlayout alloc] [init]]; //Initialize, specify the layout

There is also a way to construct a custom cell

-(ID) initWithFrame: (CGRect) frame

{

self = [super Initwithframe:frame];

if (self) {

Uiimageview *imageview = [[Uiimageview alloc]init];

Imageview.frame = CGRectMake (5, 5, 40, 40);

[Self addsubview:imageview];

_imagev = ImageView;

}

return self;

}

Note: Storyboard dragged out of the Collectionviewcontroller default is the flow layout

Similarities and differences of four registration methods

-(void) RegisterClass: (Class) Cellclass forcellwithreuseidentifier: (NSString *) identifier;

-(void) registernib: (uinib *) nib Forcellwithreuseidentifier: (NSString *) identifier;

-(void) RegisterClass: (Class) Viewclass forsupplementaryviewofkind: (NSString *) Elementkind Withreuseidentifier: ( NSString *) identifier;

-(void) registernib: (uinib *) nib Forsupplementaryviewofkind: (NSString *) kind withreuseidentifier: (NSString *) Identifier

Cell is the 1th type of code creation;

If the cell is created by Xib, use the 2nd type;

The following 2 are used to register a supplemental view, a supplemental view similar to TableView's sectionheader,sectionfooter, which distinguishes the supplemental view header from the Elementkind parameter or complements the end of the view. tail uicollectionelementkindsectionfooter head uicollectionelementkindsectionheader

Use of IOS Uicollectionview (created with storyboard and xib)

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.