ios实现下拉刷新,上拉加载
Posted 回忆12
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.
}
以上是关于ios实现下拉刷新,上拉加载的主要内容,如果未能解决你的问题,请参考以下文章