#define Rgbacolor (R, G, B, a) [Uicolor colorwithred: (r)/255.0f Green: (g)/255.0f Blue: (b)/255.0f Alpha: (a)]
#define USERCOLLECTIONVIEWCELLID @ "Rrusercollectionviewcell"
static NSString *key1 = @ "Rrheadercollectionviewcell";
static Nsinteger i = 0;
@interface Mycollectionviewcontroller () <UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, copy) NSString *kind;
@property (nonatomic, strong) Nsarray *products;
@end
@implementation Mycollectionviewcontroller
Static NSString * Const Reuseidentifier = @ "Cell";
-(Nsarray *) products{
if (_products = = Nil | | i = = 1) {
NSString *path = [[NSBundle mainbundle] pathforresource:@ "collectionviewlict.plist" oftype:nil];
Nsarray *dictarray = [Nsarray Arraywithcontentsoffile:path];
Turn a dictionary into a model
Nsmutablearray *productarray = [Nsmutablearray array];
For (Nsdictionary *dict in Dictarray) {
Rrhomepacespeedaudiencemodel *p = [Rrhomepacespeedaudiencemodel usermodelwithdict:dict];
[Productarray Addobject:p];
}
_products = [_products Arraybyaddingobjectsfromarray:productarray];
}
return _products;
}
-(void) Viewdidload {
[Super Viewdidload];
_products = [Nsmutablearray array];
[Self Configui];
uinib *nib = [uinib nibwithnibname:@ "Rrusercollectionviewcell" bundle:nil];
[Self.collectionview registernib:nib Forcellwithreuseidentifier:usercollectionviewcellid];
[Self.collectionview registernib:[uinib nibwithnibname:@ "Rrheadercollectionviewcell" Bundle:nil] Forsupplementaryviewofkind:uicollectionelementkindsectionheader withreuseidentifier:@ " Rrheadercollectionviewcell "];
[Self.collectionview Registerclass:[self.collectionview.header class] Forsupplementaryviewofkind: Uicollectionelementkindsectionheader Withreuseidentifier:key1];
self.collectionview.delegate=self;
self.collectionview.datasource=self;
}
-(void) configui{
__weak typeof (self) weakself = self;
Add a traditional pull-up refresh
[Self.collectionview addlegendfooterwithrefreshingblock:^{
[Weakself Loadmoredata];
This block is automatically called when the refresh State is entered
}];
}
-(void) loadmoredata{
i = 1;
Self.products = self.products;
[Self.collectionview Reloaddata];
[Self endrefreshing];
i = 0;
}
Start Refresh
-(void) beginrefreshing{
[Self.collectionView.footer beginrefreshing];
}
End Refresh
-(void) endrefreshing{
[Self.collectionView.footer endrefreshing];
}
-(ID) init{
1. Flow layout
Uicollectionviewflowlayout *layout = [[Uicollectionviewflowlayout alloc] init];
2. Size of each cell
Layout.itemsize = Cgsizemake (60, 60);
3. Set the horizontal spacing between cells
Layout.minimuminteritemspacing = 40;
4. Set the vertical spacing between cells
layout.minimumlinespacing = 20;
5. Set the inner margin around
Layout.sectioninset = Uiedgeinsetsmake (layout.minimumlinespacing,layout.minimuminteritemspacing, layout.minimumlinespacing,layout.minimuminteritemspacing);
return [Super Initwithcollectionviewlayout:layout];
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
}
#pragma Mark <UICollectionViewDataSource> Header
-(Cgsize) CollectionView: (Uicollectionview *) CollectionView layout: (uicollectionviewlayout*) collectionviewlayout Referencesizeforheaderinsection: (Nsinteger) section{
Return Cgsizemake ([UIScreen mainscreen].bounds.size.width, 154);//wide Default
}
-(Nsinteger) Numberofsectionsincollectionview: (Uicollectionview *) CollectionView {
return 1;
}
-(Nsinteger) CollectionView: (Uicollectionview *) CollectionView numberofitemsinsection: (NSInteger) Section {
return self.products.count;
}
-(Uicollectionviewcell *) CollectionView: (Uicollectionview *) CollectionView Cellforitematindexpath: (NSIndexPath *) Indexpath {
1. Get the cell
Rrusercollectionviewcell *cell = [CollectionView dequeuereusablecellwithreuseidentifier:usercollectionviewcellid Forindexpath:indexpath];
2. Transfer model
Cell.usermodel = Self.products[indexpath.item];
return cell;
}
-(Uicollectionreusableview *) CollectionView: (Uicollectionview *) CollectionView Viewforsupplementaryelementofkind :(NSString *) kind Atindexpath: (Nsindexpath *) indexpath{
[Self.collectionview registernib:[uinib nibwithnibname:@ "Rrheadercollectionviewcell" Bundle:nil] Forsupplementaryviewofkind:uicollectionelementkindsectionheader withreuseidentifier:@ " Rrheadercollectionviewcell "];
Uicollectionreusableview *view = [Self.collectionview dequeuereusablesupplementaryviewofkind:kind withreuseidentifier:@ "Rrheadercollectionviewcell" Forindexpath:indexpath];
return view;
}
#pragma Mark <UICollectionViewDelegate>
-(void) touchesended: (Nsset *) touches withevent: (uievent *) event{
[Super Touchesended:touches Withevent:event];
}
Use of IOS Uicollectionview