#import <UIKit/UIKit.h>@interface appdelegate:uiresponder <UIApplicationDelegate> *window; @end
#import "AppDelegate.h"#import "RootViewController.h"@interfaceappdelegate ()@end@implementationappdelegate-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions {Self.window=[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]; //Override point for customization after application launch.Self.window.backgroundColor =[Uicolor Whitecolor]; Self.window.rootViewController=[[Rootviewcontroller alloc] init]; [Self.window makekeyandvisible]; returnYES;}@end
#import <UIKit/UIKit.h>@interface rootviewcontroller:uiviewcontroller@end
#import "RootViewController.h"#import "ImageViewCell.h"#defineScreen_width [UIScreen mainscreen].bounds.size.width#definescreen_height [UIScreen mainscreen].bounds.size.height#defineLine_gap 10#defineInteritem_gap 20@interfaceRootviewcontroller () <uicollectionviewdatasource,uicollectionviewdelegate,uicollectionviewdelegateflowlayout >{}@endStaticNSString *identifier =@"Cell";StaticNSString *headeridentifier =@"Header";StaticNSString *flooteridentifier =@"Floot";@implementationRootviewcontroller- (void) viewdidload {[Super viewdidload]; //creating a Layout objectUicollectionviewflowlayout *layout =[[Uicollectionviewflowlayout alloc] init]; //set the direction of scrolling[Layout setscrolldirection:uicollectionviewscrolldirectionvertical]; //the gap of the lineLayout.minimumlinespacing =Line_gap; //Gap of ColumnLayout.minimuminteritemspacing =Ten; //size of itemLayout.itemsize = Cgsizemake ((Screen_width-2*LINE_GAP)/3, the); //Create CollectionViewUicollectionview *collectionview =[[Uicollectionview alloc] initwithframe:[[uiscreen mainscreen] bounds] collectionviewlayout:layout]; Collectionview.backgroundcolor=[Uicolor Greencolor]; //Set up proxyCollectionview.datasource =Self ; CollectionView.Delegate=Self ; //tell the system what cell to create in the future (you must register a cell in the system before getting the cell)[CollectionView Registerclass:[imageviewcellclass] Forcellwithreuseidentifier:identifier]; //Register Head View[CollectionView Registerclass:[uicollectionreusableviewclass] Forsupplementaryviewofkind:uicollectionelementkindsectionheader Withreuseidentifier:headeridentifier]; //Register Tail View[CollectionView Registerclass:[uicollectionreusableviewclass] Forsupplementaryviewofkind:uicollectionelementkindsectionfooter Withreuseidentifier:flooteridentifier]; [Self.view Addsubview:collectionview];}//tell the system how many groups there are.-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) collectionview{return 1;}//tell the system how many lines are in section group-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (nsinteger) section{return -;}//tells the system Indexpath what the Item line of section group is showing-(uicollectionviewcell*) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{Imageviewcell*cell =[CollectionView dequeuereusablecellwithreuseidentifier:identifier Forindexpath:indexpath]; Cell.photo.image= [UIImage imagenamed:@"Cellphoto"]; returncell;}//returns the size of the head Headerview-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) collectionviewlayout referencesizeforheaderinsection: (nsinteger) section{cgsize size= Cgsizemake (Screen_width, -+line_gap); returnsize;}//returns the size of the head Flooterview-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (Uicollectionviewlayout *) collectionviewlayout referencesizeforfooterinsection: (nsinteger) section{cgsize size= Cgsizemake (Screen_width, -+line_gap); returnsize;}//the contents of the head and tail display-(uicollectionreusableview*) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) indexpath{Uicollectionreusableview*reusableview =Nil; if(Kind = =Uicollectionelementkindsectionheader) {Reusableview=[CollectionView Dequeuereusablesupplementaryviewofkind:uicollectionelementkindsectionheader Withreuseidentifier:headeridentifier Forindexpath:indexpath]; Uiimageview*headerphoto = [[Uiimageview alloc] Initwithframe:cgrectmake (0,0, Screen_width, -)]; Headerphoto.image= [UIImage imagenamed:@"Headerphoto"]; [Reusableview Addsubview:headerphoto]; }Else if(Kind = =uicollectionelementkindsectionfooter) {Reusableview=[CollectionView Dequeuereusablesupplementaryviewofkind:uicollectionelementkindsectionfooter Withreuseidentifier:flooteridentifier Forindexpath:indexpath]; Uiimageview*flooterphoto = [[Uiimageview alloc] Initwithframe:cgrectmake (0, Line_gap, Screen_width, -)]; Flooterphoto.image= [UIImage imagenamed:@"Flooterphoto"]; [Reusableview Addsubview:flooterphoto]; } returnReusableview;}- (void) CollectionView: (Uicollectionview *) CollectionView Didselectitematindexpath: (Nsindexpath *) indexpath{NSLog (@"==%lu", Indexpath.row);}@end
#import <uikit/uikit.h> @interface Imageviewcell:uicollectionviewcell@property (nonatomic, Strong) Uiimageview /span>*photo; @end
#import " ImageViewCell.h " @implementation Imageviewcell- (Instancetype) initWithFrame: (cgrect) frame{ if (self = [super Initwithframe:frame]) { = [[Uiimageview alloc] init]; = self.bounds; [Self AddSubview:self.photo]; } return Self ;} @end
IOS Uicollectionview III (Basic usage)