iOS implementation drop-down refresh, pull-up load

Source: Internet
Author: User

1. Custom cell

#import "EnjoyListModel.h"

@protocol enjoycollectioncellofviewdelegate <NSObject>

-(void) Freebuttondelegateaction: (UIButton *) sender;

@end

@interface Enjoycollectioncellofview:uicollectionviewcell

@property (Nonatomic,strong) id<enjoycollectioncellofviewdelegate>delegate;

/* Free Button * * *

@property (Nonatomic,strong) UIButton *freebutton;

-(void) Updateenjoycollectioncellofviewmodel: (Enjoylistmodel *) model;

@end

. m file implementations

#import "EnjoyCollectionCellOfView.h"

#import "Masonry.h"

#import "Uiimageview+webcache.h"

#define ENJOYCOLLECTIONVIEWLEFTMARGIN 0

#define ENJOYCOLLECTIONVIEWTOPMARGIN 0

@interface Enjoycollectioncellofview ()

/* Product Picture */

@property (Nonatomic,strong) Uiimageview *goodsimageview;

/* Product Name */

@property (Nonatomic,strong) UILabel *goodstitlelabel;

/* Earnings and names */

@property (Nonatomic,strong) UILabel *goodslastlabel;

@end

@implementation Enjoycollectioncellofview

-(Instancetype) initWithFrame: (CGRect) frame

{

self = [super Initwithframe:frame];

if (self)

{

Self.backgroundcolor = [Uicolor Whitecolor];

__weak __typeof (&*self) weakself = self;

[Self.contentview AddSubview:self.goodsImageView];

[Self.goodsimageview mas_makeconstraints:^ (Masconstraintmaker *make)

{

Make.left.mas_equalTo (weakself.mas_left). Offset (enjoycollectionviewleftmargin);

Make.top.mas_equalTo (weakself.mas_top). Offset (enjoycollectionviewtopmargin);

Make.width.mas_equalTo (weakSelf.contentView.mas_width). Offset (0);

Make.height.mas_equalTo (weakSelf.contentView.mas_width). Offset (0);

}];

[Self.contentview AddSubview:self.goodsTitleLabel];

[Self.goodstitlelabel mas_makeconstraints:^ (Masconstraintmaker *make)

{

Make.left.mas_equalTo (weakself.mas_left). Offset (enjoycollectionviewleftmargin);

Make.right.mas_equalTo (weakself.mas_right). Offset (-enjoycollectionviewleftmargin);

Make.top.mas_equalTo (WeakSelf.goodsImageView.mas_bottom). Offset (0);

Make.height.mas_offset (20);

}];

[Self.contentview AddSubview:self.goodsLastLabel];

[Self.goodslastlabel mas_makeconstraints:^ (Masconstraintmaker *make)

{

Make.left.mas_equalTo (weakself.mas_left). Offset (enjoycollectionviewleftmargin);

Make.right.mas_equalTo (weakself.mas_right). Offset (-enjoycollectionviewleftmargin);

Make.top.mas_equalTo (WeakSelf.goodsTitleLabel.mas_bottom). Offset (0);

Make.height.mas_offset (20);

}];

[Self.contentview AddSubview:self.freeButton];

[Self.freebutton mas_makeconstraints:^ (Masconstraintmaker *make)

{

Make.left.mas_equalTo (weakself.mas_left). Offset (35);

Make.right.mas_equalTo (weakself.mas_right). Offset (-35);

Make.bottom.mas_equalTo (WeakSelf.contentView.mas_bottom). Offset (-10);

Make.height.mas_offset (30);

}];

[Self.freebutton addtarget:self Action: @selector (freebuttonaction:) forcontrolevents:uicontroleventtouchupinside] ;

}

return self;

}

#pragma mark-buttonaction

-(void) Freebuttonaction: (UIButton *) sender

{

if ([Self.delegate respondstoselector: @selector (freebuttondelegateaction:)])

{

[Self.delegate Freebuttondelegateaction:sender];

}

}

#pragma mark-updateui

#pragma mark--lazy

-(Uiimageview *) Goodsimageview

{

if (!_goodsimageview)

{

_goodsimageview = [[Uiimageview alloc]init];

_goodsimageview.backgroundcolor = [Uicolor Redcolor];

}

return _goodsimageview;

}

-(UILabel *) Goodstitlelabel

{

if (!_goodstitlelabel) {

_goodstitlelabel = [[UILabel alloc]init];

_goodstitlelabel.text = @ "Product name";

_goodstitlelabel.backgroundcolor = [Uicolor Lightgraycolor];

_goodstitlelabel.textalignment = Nstextalignmentcenter;

_goodstitlelabel.font = [Uifont systemfontofsize:12];

}

return _goodstitlelabel;

}

-(UILabel *) Goodslastlabel

{

if (!_goodslastlabel) {

_goodslastlabel = [[UILabel alloc]init];

_goodslastlabel.text = @ "Product name";

_goodslastlabel.backgroundcolor = [Uicolor Yellowcolor];

_goodslastlabel.textalignment = Nstextalignmentcenter;

_goodslastlabel.font = [Uifont systemfontofsize:12];

}

return _goodslastlabel;

}

-(UIButton *) Freebutton

{

if (!_freebutton)

{

_freebutton = [UIButton buttonwithtype:uibuttontypecustom];

[_freebutton settitle:@ "readily take" forstate:uicontrolstatenormal];

[_freebutton Settitlecolor:[uicolor Blackcolor] forstate:uicontrolstatenormal];

[_freebutton Setbackgroundcolor:[uicolor Redcolor];

_freebutton.layer.maskstobounds = YES;

_freebutton.layer.cornerradius = 5;

}

return _freebutton;

}

@end

which

Enjoywaterflowlayout is the same as my last article, you can go and watch

Then my collectionview is placed in a view, not put in the VC

#import <UIKit/UIKit.h>

@protocol zoneofenjoyviewdelegate <NSObject>

-(void) Freebtngotobuyview: (UIButton *) sender Withlistmodel: (Enjoylistmodel *) Listmodel;

@end

@interface Zoneofenjoyview:uiview

@property (nonatomic,assign) id<zoneofenjoyviewdelegate>delegate;

@property (Nonatomic,strong) Uicollectionview *collectionview;

/* Comment */

@property (Nonatomic,strong) Nsmutablearray *goodsmuarray;

@end

. m file implementations

#define KMAINSCREENWIDTH [[UIScreen Mainscreen] Applicationframe].size.width

#define Kmainscreenheight [[UIScreen Mainscreen] Applicationframe].size.height

#define KDEVICEWIDTH [UIScreen mainscreen].bounds.size.width//Interface width

#define Kdeviceheight [UIScreen mainscreen].bounds.size.height//interface height

#define Kstatusbarheight [[UIApplication sharedapplication] Statusbarframe].size.height

#define Kheight (kdeviceheight-kstatusbarheight-knavigationbarheight)//height

#define Ktabbarheight 49.0

#import "ZoneOfEnjoyView.h"

#import "EnjoyBannerView.h"

#import "EnjoyCollectionCellOfView.h"

#import "EnjoyWaterFlowLayout.h"

@interface Zoneofenjoyview () <uicollectionviewdelegate,uicollectionviewdatasource,waterflowlayoutdelegate, Enjoycollectioncellofviewdelegate>

{

}

@property (Nonatomic,strong) Enjoybannerview *bannerview;

/* Comment */

@property (Nonatomic,strong) Nsmutablearray *goodsmuarray;

@end

static NSString *const cellidentifier = @ "Enjoycollectioncellofviewcell";

@implementation Zoneofenjoyview

-(Instancetype) initWithFrame: (CGRect) frame

{

self = [super Initwithframe:frame];

if (self)

{

Enjoywaterflowlayout *layout = [[Enjoywaterflowlayout alloc]init];

Layout.delegate = self;

Self.collectionview = [[Uicollectionview alloc]initwithframe:cgrectmake (0, 0, Kdevicewidth, Kdeviceheight-10-ktabbar Height-44) Collectionviewlayout:layout];

Self.collectionView.backgroundColor = [Uicolor Whitecolor];

_collectionview.datasource = self;

_collectionview.delegate = self;

[Self addSubview:self.collectionView];

[Self.collectionview Registerclass:[enjoycollectioncellofview class] forcellwithreuseidentifier:cellidentifier];

if (!self.goodsmuarray)

//        {

Self.goodsmuarray = [Nsmutablearray array];

//        }

}

return self;

}

-(void) Setgoodsmuarray: (Nsmutablearray *) Goodsmuarray

{

_goodsmuarray = Goodsmuarray;

if (_goodsmuarray.count)

{

[Self.goodsmuarray Addobjectsfromarray:goodsmuarray];

[Self.collectionview Reloaddata];

}

Else

{

Return

}

}

#pragma mark--Uicollectionviewdatasource

#pragma mark-collection data Source proxy

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

{

if (Self.goodsMuArray.count)

{

return self.goodsMuArray.count;

}

Else

{

return 0;

}

}

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

{

Enjoylistmodel *model = [[Enjoylistmodel alloc]init];

Enjoycollectioncellofview *cell = (Enjoycollectioncellofview *) [CollectionView Dequeuereusablecellwithreuseidentifier:cellidentifier Forindexpath:indexpath];

Cell.freeButton.tag = Indexpath.row;

Cell.delegate = self;

if (Self.goodsMuArray.count)

{

model = Self.goodsmuarray[indexpath.row];

[Cell Updateenjoycollectioncellofviewmodel:model];

}

Else

{

}

return cell;

}

-(CGFloat) Waterflowlayout: (Enjoywaterflowlayout *) waterflowlayout Heightforrowatindexpath: (NSInteger) index Itemwidth: (cgfloat) Itemwidth

{

Return (Kdevicewidth)/2 + 75;

}

@end

Finally in the VC implementation

#import "ViewController.h"

#import "ZoneOfEnjoyView.h"

#import "MJREFRESH.H"

#define KDEVICEWIDTH [UIScreen mainscreen].bounds.size.width//Interface width

#define Kdeviceheight [UIScreen mainscreen].bounds.size.height//interface height

@interface Viewcontroller ()

/* Comment */

@property (Nonatomic,strong) Zoneofenjoyview *enjoyview;

/**

* Store fake data

*/

@property (Strong, nonatomic) Nsmutablearray *fakecolors;

@end

@implementation Viewcontroller

-(void) Viewdidload {

[Super Viewdidload];

Self.enjoyview = [[Zoneofenjoyview alloc]initwithframe:cgrectmake (0, 0, Kdevicewidth, kDeviceHeight-45)];

[Self.view AddSubview:self.enjoyView];

if (!self.fakecolors) {

Self.fakecolors = [Nsmutablearray array];

}

Self.enjoyView.goodsMuArray = [Nsmutablearray array];

Additional setup after loading the view, typically from a nib.

}

-(void) Viewwillappear: (BOOL) animated

{

Nsarray *array [email protected][@ "1111", @ "2222", @ "1111"];

[Self.enjoyview Resetzoneofenjoyview:array withindex:1];

2. Integrated Refresh Control

[Self addheader];

[Self addfooter];

}

-(void) AddHeader

{

__unsafe_unretained typeof (self) VC = self;

__block typeof (self) weakself = self;

Add a drop-down refresh header control

Nsmutablearray *array = [Nsmutablearray array];

[WeakSelf.enjoyView.collectionView addheaderwithcallback:^{

Enter the refresh state and the block will be recalled.

Add 5 false data

for (int i = 0; i<5; i++)

{

[WeakSelf.enjoyView.goodsMuArray insertobject:@ "0000" atindex:0];

}

Simulates lazy loading of data, so it is only called after 2 seconds)

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{

[Vc.enjoyView.collectionView Reloaddata];

End Refresh

[Vc.enjoyView.collectionView headerendrefreshing];

});

}];

#warning automatic Refresh (a drop-down refresh when you enter the program)

[Self.enjoyView.collectionView headerbeginrefreshing];

}

-(void) Addfooter

{

__unsafe_unretained typeof (self) VC = self;

__block typeof (self) weakself = self;

Add a pull-up refresh trailer control

[WeakSelf.enjoyView.collectionView addfooterwithcallback:^{

Enter the refresh state and the block will be recalled.

Add 5 false data

for (int i = 0; i<5; i++) {

[WeakSelf.enjoyView.goodsMuArray addobject:@ "1111"];

}

[Weakself.enjoyview Resetzoneofenjoyview:[nsarray arrayWithArray:vc.fakeColors] withindex:0];

Simulates lazy loading of data, so it is only called after 2 seconds)

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{

[Vc.enjoyView.collectionView Reloaddata];

End Refresh

[Vc.enjoyView.collectionView footerendrefreshing];

});

}];

}

/**

Frees up memory in Dealloc in order to ensure internal non-disclosure

*/

-(void) dealloc

{

NSLog (@ "Mjcollectionviewcontroller--dealloc---");

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

iOS implementation drop-down refresh, pull-up load

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.