BaseCollectionViewController的封装github记得star 哦

针对网上缺少BaseCollectionViewController的封装,作者进行了个人的实现并分享到GitHub,仓库地址为:https://github.com/liuheisBest/LHExtions,感兴趣的同学可以关注和star。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上查了一下没有 所以自己封装了一个

#import <UIKit/UIKit.h>
#import "BaseViewController.h"
@interface BaseCollectionViewController : BaseViewController
//页数索引
@property (nonatomic,assign) NSInteger pageNO;
//每页显示多少条
@property (nonatomic,assign) NSInteger pageSize;
//数据源
@property (nonatomic,strong) NSMutableArray *dataArray;
//开启头部刷新
@property (nonatomic,assign) BOOL  isOpenHeaderRefresh;
//开启脚部刷新
@property (nonatomic,assign) BOOL  isOpenFooterRefresh;

@property(strong,nonatomic)UICollectionView * collectionView;
@property(strong,nonatomic)UICollectionViewFlowLayout *flowLayout;

//头部刷新请求 (子类需要重写)
- (void)headerRequestWithData;

- (void)footerRequestWithData;
@end


#import "BaseCollectionViewController.h"
#define MJRandomColor [UIColor colorWithRed:arc4random_uniform(255)/255.0 green:arc4random_uniform(255)/255.0 blue:arc4random_uniform(255)/255.0 alpha:1]
@interface BaseCollectionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>


@end

@implementation BaseCollectionViewController
static NSString * reuseIdentifier = @"hehe";
- (UICollectionViewFlowLayout *)flowLayout{
    if (!_flowLayout) {
        UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
        //设置流水布局的大小
        CGFloat cellWith =([UIScreen mainScreen].bounds.size.width)/3.0;
        flowLayout.itemSize = CGSizeMake(cellWith , cellWith);
        flowLayout.minimumInteritemSpacing = 0;
        flowLayout.minimumLineSpacing = 0;
        _flowLayout = flowLayout;
    }
    return _flowLayout;
}
- (UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:self.flowLayout];
        /**
         *默认是YES ,设置为NO之后就不在滚动,不会出现上下拉刷新
         */
        collectionView.scrollEnabled = YES;
        collectionView.translatesAutoresizingMaskIntoConstraints = YES;
        [self.view addSubview:collectionView];
        collectionView.collectionViewLayout = self.flowLayout;
        collectionView.delegate =self;
        collectionView.dataSource =self;
        collectionView.backgroundColor = [UIColor clearColor];
        [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
        _collectionView = collectionView;
    }
    return _collectionView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageNO = 1;
    self.pageSize = 15;
    [self.view addSubview:self.collectionView];

}
- (void)setIsOpenHeaderRefresh:(BOOL)isOpenHeaderRefresh{
    if (_isOpenHeaderRefresh != isOpenHeaderRefresh) {
        _isOpenHeaderRefresh = isOpenHeaderRefresh;
        WeakSelf(self);
        if (isOpenHeaderRefresh) {
            self.collectionView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
                // 增加5条假数据
                weakSelf.pageNO = 1;
                [weakSelf headerRequestWithData];
                // 结束刷新
                [weakSelf.collectionView.mj_header endRefreshing];
                
            }];
            [self.collectionView.mj_header beginRefreshing];
        
        }
    }
}
//下拉刷新
- (void)setIsOpenFooterRefresh:(BOOL)isOpenFooterRefresh{
    if (_isOpenFooterRefresh != isOpenFooterRefresh) {
        _isOpenFooterRefresh = isOpenFooterRefresh;
        WeakSelf(self);
        if (isOpenFooterRefresh) {
            // 上拉刷新
            self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
                // 增加5条假数据
                weakSelf.pageNO ++;
                [weakSelf footerRequestWithData];
                // 结束刷新
                [weakSelf.collectionView.mj_footer endRefreshing];
            }];
        }
    }
}
//头部刷新请求 (子类需要重写)
- (void)headerRequestWithData{
    
}
//脚部刷新
- (void)footerRequestWithData{
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UICollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    cell.contentView.backgroundColor = self.dataArray[indexPath.row];
    
    return cell;
}


//- (UIEdgeInsets )collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:( NSInteger )section
//
//{
//    return UIEdgeInsetsMake (10,10,0,10);
//}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end


demo地址:自己花时间写了一上午,也不容易,你懂得

https://github.com/liuheisBest/LHExtions

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值