ios实现下拉刷新,上拉加载

1、自定义cell

//#import "EnjoyListModel.h"

@protocol EnjoyCollectionCellOfViewDelegate <NSObject>

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

@end

@interface EnjoyCollectionCellOfView : UICollectionViewCell

@property (nonatomic,strong)id<EnjoyCollectionCellOfViewDelegate>delegate;

/*免费拿按钮*/

@property (nonatomic,strong)UIButton *freeButton;

//- (void)updateEnjoyCollectionCellOfViewModel:(EnjoyListModel *)model;

@end

.m文件实现

#import "EnjoyCollectionCellOfView.h"

#import "Masonry.h"

//#import "UIImageView+WebCache.h"

#define EnjoyCollectionViewLeftMargin 0

#define EnjoyCollectionViewTopMargin  0

@interface EnjoyCollectionCellOfView ()

/*商品图片*/

@property (nonatomic,strong)UIImageView *goodsImageView;

/*商品名称*/

@property (nonatomic,strong)UILabel *goodsTitleLabel;

/*收益和名称*/

@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 = @"商品名称";

//        _goodsTitleLabel.backgroundColor = [UIColor lightGrayColor];

_goodsTitleLabel.textAlignment = NSTextAlignmentCenter;

_goodsTitleLabel.font = [UIFont systemFontOfSize:12];

}

return _goodsTitleLabel;

}

- (UILabel *)goodsLastLabel

{

if (!_goodsLastLabel) {

_goodsLastLabel = [[UILabel alloc]init];

_goodsLastLabel.text = @"商品名称";

//        _goodsLastLabel.backgroundColor = [UIColor yellowColor];

_goodsLastLabel.textAlignment = NSTextAlignmentCenter;

_goodsLastLabel.font = [UIFont systemFontOfSize:12];

}

return _goodsLastLabel;

}

- (UIButton *)freeButton

{

if (!_freeButton)

{

_freeButton = [UIButton buttonWithType:UIButtonTypeCustom];

[_freeButton setTitle:@"随手拿" forState:UIControlStateNormal];

//        [_freeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[_freeButton setBackgroundColor:[UIColor redColor]];

_freeButton.layer.masksToBounds = YES;

_freeButton.layer.cornerRadius = 5;

}

return _freeButton;

}

@end

其中

EnjoyWaterFlowLayout与我上篇是一样的,可以前去观看

然后我的collectionView是放在一个view中的,不是放在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;

/*注释*/

@property (nonatomic,strong)NSMutableArray *goodsMuArray;

@end

.m文件实现

#define kMainScreenWidth    [[UIScreen mainScreen] applicationFrame].size.width

#define kMainScreenHeight   [[UIScreen mainScreen] applicationFrame].size.height

#define kDeviceWidth        [UIScreen mainScreen].bounds.size.width      // 界面宽度

#define kDeviceHeight       [UIScreen mainScreen].bounds.size.height     // 界面高度

#define kStatusBarHeight    [[UIApplication sharedApplication] statusBarFrame].size.height

#define kHeight             (kDeviceHeight - kStatusBarHeight - kNavigationBarHeight)   // 高度

#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;

/*注释*/

//@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 - kTabBarHeight - 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数据源代理

- (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

最后在VC中实现

#import "ViewController.h"

#import "ZoneOfEnjoyView.h"

#import "MJRefresh.h"

#define kDeviceWidth        [UIScreen mainScreen].bounds.size.width      // 界面宽度

#define kDeviceHeight       [UIScreen mainScreen].bounds.size.height     // 界面高度

@interface ViewController ()

/*注释*/

@property (nonatomic,strong)ZoneOfEnjoyView *enjoyView;

/**

*  存放假数据

*/

@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];

// Do any 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.集成刷新控件

[self addHeader];

[self addFooter];

}

- (void)addHeader

{

__unsafe_unretained typeof(self) vc = self;

__block typeof(self)WeakSelf = self;

// 添加下拉刷新头部控件

NSMutableArray *array = [NSMutableArray array];

[WeakSelf.enjoyView.collectionView addHeaderWithCallback:^{

// 进入刷新状态就会回调这个Block

// 增加5条假数据

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

{

[WeakSelf.enjoyView.goodsMuArray insertObject:@"0000" atIndex:0];

}

// 模拟延迟加载数据,因此2秒后才调用)

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

[vc.enjoyView.collectionView reloadData];

// 结束刷新

[vc.enjoyView.collectionView headerEndRefreshing];

});

}];

#warning 自动刷新(一进入程序就下拉刷新)

[self.enjoyView.collectionView headerBeginRefreshing];

}

- (void)addFooter

{

__unsafe_unretained typeof(self) vc = self;

__block typeof(self)WeakSelf = self;

// 添加上拉刷新尾部控件

[WeakSelf.enjoyView.collectionView addFooterWithCallback:^{

// 进入刷新状态就会回调这个Block

// 增加5条假数据

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

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

}

//       [WeakSelf.enjoyView resetZoneOfEnjoyView:[NSArray arrayWithArray:vc.fakeColors] withIndex:0];

// 模拟延迟加载数据,因此2秒后才调用)

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

[vc.enjoyView.collectionView reloadData];

// 结束刷新

[vc.enjoyView.collectionView footerEndRefreshing];

});

}];

}

/**

为了保证内部不泄露,在dealloc中释放占用的内存

*/

- (void)dealloc

{

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

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

时间: 2024-12-07 13:39:48

ios实现下拉刷新,上拉加载的相关文章

最新Android ListView 下拉刷新 上滑加载

开发项目过程中基本都会用到listView的下拉刷新和上滑加载更多,之前大家最常用的应该是pull to refresh或它的变种版吧,google官方在最新的android.support.v4包中增加了一个新类SwipeRefreshLayout,地址 这个类的作用就是提供官方的下拉刷新,并且效果相当不错,而上拉加载更多则用我们自定义的listview,也是相当简单. 下拉刷新 简单的介绍下: 首先它是一个viewgroup,但是它只允许有一个子控件,子控件能是任何view,使用的时候,所在

十分钟实现ListView下拉刷新上滑加载更多

说到ListView下拉刷新几乎每个APP都会用到,所以ListView下拉刷新是很重要的,就像ListView优化一样是你必会的东西. ListView实现下拉刷新如果我们开发人员自己编写相对来说比较费事的,当我们使用第三方库之后我们再来开发这个功能就会省事很多.相比与自己实现可以少编写不少代码,Android-PullToRefresh库可以轻松实现ListView的下拉刷新功能. 要使用Android—PullToRefesh库对ListView实现下拉刷新要经过以下几个步骤: 1.下载A

Android 下拉刷新上啦加载SmartRefreshLayout + RecyclerView

在弄android刷新的时候,可算是耗费了一番功夫,最后发觉有现成的控件,并且非常好用,这里记录一下. 原文是 https://blog.csdn.net/huangxin112/article/details/78781682 ,这里是看了之后,结合自己实际遇到的问题写的. 首先引入包. //下拉框 implementation 'com.android.support:recyclerview-v7:28.0.0-beta01' implementation 'com.scwang.smar

android 下拉刷新上拉加载更多,高仿ios左滑动删除item,解决了众多手势问题

一.前言 老规矩,别的不说,这demo是找了很相关知识集合而成的,可以说对我这种小白来说是绞尽脑汁!程序员讲的是无图无真相!现在大家一睹为快! 二.比较关键的还是scroller这个类的 package com.icq.slideview.view; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; i

IOS开发之XML解析以及下拉刷新上拉加载更多的分享

IOS开发之XML解析 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示XML文件版本, 内部文本使用的编码 <root> 表示根节点 <CityName>北京</CityName>  一个结点, CityName是结点名, 北京结点值 <Item key="1" value="A"></Item>  

XML解析以及下拉刷新上拉加载的一些基本使用

---恢复内容开始--- IOS开发之XML解析 1.XML格式 <?xml version="1.0" encoding="utf-8" ?> 表示XML文件版本, 内部文本使用的编码 <root> 表示根节点 <CityName>北京</CityName>  一个结点, CityName是结点名, 北京结点值 <Item key="1" value="A"><

IOS学习之UiTableView下拉刷新与自动加载更多,百年不变的效果

IOS学习之UiTableView下拉刷新与自动加载更多,百年不变的效果(五) 五一劳动节马上来临,小伙伴有妹有很激动哟,首先祝天下所有的程序猿节日快乐!这个五一对于我来说有点不一样,我的人生从这个五一就转弯了,爱情长跑8年的我结婚了,一会支付宝账号我会公布出去,请自觉打款!谢谢合作. 灯光闪起来: 舞蹈跳起来: 歌曲唱起来: -------------------------------------------------------------------------------------

自个儿写Android的下拉刷新/上拉加载控件 (续)

本文算是对之前的一篇博文<自个儿写Android的下拉刷新/上拉加载控件>的续章,如果有兴趣了解更多的朋友可以先看一看之前的这篇博客. 事实上之所以会有之前的那篇博文的出现,是起因于前段时间自己在写一个练手的App时很快就遇到这种需求.其实我们可以发现类似这样下拉刷新.上拉加载的功能正在变得越来越普遍,可以说如今基本上绝大多数的应用里面都会使用到.当然,随着Android的发展,已经有不少现成的可以实现这种需求的"轮子"供我们使用了. 但转过头想一下想,既然本来就是自己练手

Maxwin-z/XListView-Android(下拉刷新上拉加载)源码解析(一)

本次解析的内容,是github上一个用于下拉刷新上拉加载的控件xlistview,这个功能相信大家在开发的过程中会经常用到. 控件的源码地址是https://github.com/Maxwin-z/XListView-Android 在这个控件之前,我看过一些相同功能的控件,挑选后觉得XListView功能比较完善,而且易于理解.在android-open-project里面,有提到一个DropDownListView,个人使用过以后,觉得功能是具备了,但是操作体验不好,原因就是没有使用到Scr

android 安卓 listview 支持下拉刷新 上拉加载更多

[1]重写listView import java.text.SimpleDateFormat; import java.util.Date; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGrou