Professional nine development for several years ... You must use this control
Here is an easy "product recommendation" interface to do the introduction.
First, the use of Uicollectionview
1. Register the cell (Tell CollectionView what cell to create in the future) [Self.collectionview Registerclass:[uicollectionviewcell class] forcellwithreuseidentifier:@ "Product"];2, remove cell-(Uicollectionviewcell *) from the cache pool CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (Nsindexpath *) Indexpath{uicollectionviewcell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:@ "Product" Forindexpath:indexpath]; return cell;} 3, rewrite the Init method, create layout Parameters--"Here to rewrite the Init method is because we use the Init method in Baseviewcontroller, in order to unify, so rewrite the Init method-(ID) init{ //1. Flow layout uicollectionviewflowlayout *layout = [[ Uicollectionviewflowlayout alloc] init]; //2. Size of each cell Layout.itemsize = Cgsizemake (+); return [Super Initwithcollectionviewlayout:layout];} 4, Uicollectionviewflowlayout called "Flow Layout", used to constrain the cell display Common Properties cell size @property (nonatomic) cgsize itemsize; Horizontal spacing between cells @property (nonatomic) cgfloat minimuminteritemspacing; The vertical spacing between cells @property (nonatomic) cgfloat minimumlinespacing; four-week internal margin @property (nonatomic) uiedgeinsets sectioninset;
Second, the demo--An easy "product recommendation" interface
#import "BJProductViewController.h"
#define BJPRODUCTID @ "Product" #import "BJProductViewController.h" #import "BJProduct.h" #import "BJCollectionCell.h" @ Interface Bjproductviewcontroller () @property (Nonatomic,strong) Nsmutablearray *products; @end @implementation Bjproductviewcontrollerstatic NSString * Const Reuseidentifier = @ "Cell";-(instancetype) init{Uicollectionviewflow Layout *layout = [[Uicollectionviewflowlayout alloc]init]; Layout.itemsize = Cgsizemake (85, 85); layout.minimuminteritemspacing = 0; layout.minimumlinespacing = 20; Layout.sectioninset = Uiedgeinsetsmake (layout.minimumlinespacing,10,0,10); return [Super Initwithcollectionviewlayout:layout];} -(Instancetype) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) nibbundleornil{self = [Super initwithnibn Ame:nibnameornil Bundle:nibbundleornil]; if (self) {//Custom initialization} return to self;} -(Nsarray *) products{if (_products = = nil) {NSString *path = [[NSBundle mainbundle] pathforresource:@ "ProduCts.json "Oftype:nil"; NSData *json = [NSData Datawithcontentsoffile:path]; Nsarray *dictarray = [nsjsonserialization jsonobjectwithdata:json options:nsjsonreadingmutablecontainers Error:nil]; Nsmutablearray *mutarray = [Nsmutablearray array]; For (Nsdictionary *dict in Dictarray) {bjproduct *product = [Bjproduct productwithdict:dict]; [Mutarray addobject:product]; } _products = Mutarray; }//NSLog (@ "%@", _products); return _products;} -(void) viewdidload{[Super Viewdidload]; Self.collectionView.backgroundColor = [Uicolor Whitecolor]; Uncomment the following line to preserve selection between presentations//Self.clearsselectiononviewwillappear = N O uinib *nib = [uinib nibwithnibname:@ "Bjcollectioncell" bundle:nil]; [Self.collectionview registernib:nib Forcellwithreuseidentifier:bjproductid]; Register cell classes//[Self.collectionview registerclass:[uicollectiOnviewcell class] forcellwithreuseidentifier:reuseidentifier]; Do any additional setup after loading the view.} #pragma mark <uicollectionviewdatasource>-(Nsinteger) Numberofsectionsincollectionview: (UICollectionView *) collectionview{return 1;} -(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) section{return SE Lf.products.count;} -(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) indexpath{Bjcollectioncell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:bjproductid forIndexPath : Indexpath]; Configure the cell cell.product = Self.products[indexpath.item]; Cell.backgroundcolor = [Uicolor Orangecolor]; return cell;} @end
#import BJCollectionCell.h
#import <UIKit/UIKit.h> @class bjproduct; @interface Bjcollectioncell:uicollectionviewcell@property (nonatomic , strong) Bjproduct *product; @end
#import BJCOLLECTIONCELL.M
#import "BJCollectionCell.h" #import "BJProduct.h" @interface Bjcollectioncell () @property (weak, nonatomic) Iboutlet Uiimageview *iconimage; @property (weak, nonatomic) Iboutlet UILabel *namelable; @end @implementation bjcollectioncell-( void) awakefromnib//The xib is used here, the data is loaded here, there is a need to note the cell's parent class and id{ //initialization code Self.iconImage.layer.cornerRadius = 6; Self.iconImage.layer.masksToBounds = YES;} -(void) Setproduct: (bjproduct *) product{ _product = product; Self.iconImage.image = [UIImage ImageNamed:product.icon]; Self.nameLable.text = Product.title;} @end
#import BJProduct.h
#import <Foundation/Foundation.h> @interface bjproduct:nsobject@property (nonatomic,copy) NSString *icon;@ Property (nonatomic,copy) NSString *title;+ (instancetype) productwithdict: (Nsdictionary *) dict;+ (instancetype) Initwithdict: (nsdictionary *) dict; @end
#import BJPRODUCT.M
#import "BJProduct.h" @implementation bjproduct+ (Instancetype) productwithdict: (nsdictionary *) dict{ bjproduct * Product = [[Self alloc] init]; Product.icon = dict[@ "icon"]; Product.title = dict[@ "title"]; return product;} + (Instancetype) initwithdict: (nsdictionary *) dict{ return [self productwithdict:dict];} @end
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS Development UI article--Uicollectionview