Accustomed to the use of xib and storyboard to create Uicollectionview Project Engineering Partners, need to transform the use of pure code to achieve, to avoid touching more walls, you need to carefully understand the creation of the Uicollectionview process. Creating a Uicollectionview is more complex than creating a uitableview, and it is also a relatively peculiar way of initializing. Here's how to create a Uicollectionview using pure code.
- 1
Create project and view Controller
Create a project Uicollectionview and build a new uiviewcontroller. Select Project, right-click-new File ... Select "Cocoa Touch Class"-next, give a reasonable name Viewcontroller, then Next complete.
include #import "ViewController.h" in the APPDELEGATE.M file. Add code:
Uinavigationcontroller *NAVC = [[Uinavigationcontroller alloc]initwithrootviewcontroller:[[ Viewcontroller Alloc]init]];
Self.window.rootViewController = navc;//Sets the NAVC to the root view controller.
Modify the display style of Viewcontroller, perform compilation, run, effect.
- 2
Create custom Uicollectionviewcell
Select project, right-click-new File ... Select "Cocoa Touch class"-next, select inherit from Uicollectionviewcell class, give a reasonable name Collectionviewcell, then Next complete.
1, customize the controls you want, such as Uiimageview:
@property (nonatomic, strong) Uiimageview *imgview;
2, initialize the control, implemented in method-(ID) initWithFrame: (CGRect) Frame:
self.imgview = [[Uiimageview Alloc]initwithframe:cgrectmake (0, Max, Max)];
Self.imgView.backgroundColor = [Uicolor grouptableviewbackgroundcolor];
[self addSubview:self.imgView];
- 3
create Uicollectionview and add proxies
1, add event agent and data source Agent <UICollectionViewDataSource,UICollectionViewDelegate> in ViewController.h.
2, create Uicollectionview in VIEWCONTROLLER.M. Need to use Uicollectionviewflowlayout to create, using method - (Instancetype) initWithFrame: (CGRect) Frame collectionviewlayout :(uicollectionviewlayout *) layout; If you only use the normal Init method, it is not possible.
4, delegate authorization and add to view.
self.collectionview.delegate = self;
self.collectionView.dataSource = self;
[Self.view AddSubview:self.collectionView];
- 4
add Uicollectionviewcell to Uicollectionview
1, register Collectionviewcell, add cell needs to be implemented here. Method: -(void) RegisterClass: (Class) Cellclass forcellwithreuseidentifier: (NSString *) identifier;
2, adding proxy methods
//define the number of Uicollectionviewcell displayed
-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) section;
//define the number of sections displayed
-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView;
//What each Uicollectionview shows
-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: ( Nsindexpath *) Indexpath;
- 5
setting properties in Uicollectionview
// Define the size of each uicollectionview (return cgsize: Width and height)
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout :(uicollectionviewlayout*) collectionviewlayout sizeforitematindexpath: (Nsindexpath *) IndexPath;
//define the spacing for each uicollectionview (return uiedgeinsets: Top, left, bottom, right)
-(Uiedgeinsets) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) Collectionviewlayout Insetforsectionatindex: (nsinteger) section;
//define the longitudinal spacing for each uicollectionview
-(CGFloat) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) Collectionviewlayout Minimuminteritemspacingforsectionatindex: (nsinteger) section;
The method called when//uicollectionview is selected
-(void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) IndexPath;
end
Code