基于MVVM框架简洁性而封装的模型驱动UIViewCollecionView组件库

项目Demo地址:https://github.com/DaLiangWang/WLCollectionView

优势

更加符合MVVM框架
由VM生成CollectionView的显示视图模型,回调给View中直接进行渲染
在项目中可在绝大多数情况下避免数组越界,cell初始化问题等崩溃
快速开发利器小帮手
可无缝接入项目,无需修改已存在Cell的内容,只需添加两个方法
一个数据绑定方法,一个代理绑定方法

使用方式

pod 'WLCollectionView' , '~> 1.0.1'
或者
下载源码中的Class文件下内容拖入项目,相对cocopods导入,可做定制化功能

项目核心源码,相对简单,全部都有注释,并且无依赖性,可拆解使用

核心消息方法转发分类在 NSObject+WLSel.h 中,均有完整注释和保护措施

控制器代码

@interface ViewController ()
@property (nonatomic,strong) WLCollectionView *wlclass_collection_view;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.wlclass_collection_view.frame = self.view.frame;
    
    [self.view addSubview:self.wlclass_collection_view];
    [self reload];
}
-(void)reload{
    
    NSMutableArray *array = [NSMutableArray array];
    
    while (array.count < 5) {
        WLBaseCollectionViewLayerSection *section = [[WLBaseCollectionViewLayerSection alloc]init];
        section.insetForSection = UIEdgeInsetsMake(1, 0, 1, 0);
        section.horizontalSection = 0;
        section.horizontalCount = 1;
        section.horizontalMaxWidth = self.view.frame.size.width;
        section.verticalSection = 1;
        
        CGSize itemSize = [section getCellSizeHeight:45];

        section.headerItem = [UICollectionReusableView toClass:HeaderCollectionReusableView.class rowData:@{
            @"title":[NSString stringWithFormat:@"headerItem-%ld",array.count]
        } cellSize:itemSize];
        
        section.footItem = [UICollectionReusableView toClass:FootCollectionReusableView.class rowData:@{
            @"title":[NSString stringWithFormat:@"footItem-%ld",array.count]
        } cellSize:itemSize];

        
        while (section.item.count < 5) {
            ItemCollectionViewCellModel *model = [[ItemCollectionViewCellModel alloc]init];
            model.string = [NSString stringWithFormat:@"item-%ld-%ld",array.count,section.item.count];
            
            WLBaseCollectionViewLayerRow *row = [UICollectionViewCell toClass:ItemCollectionViewCell.class rowData:model cellSize:itemSize];
            [section.item addObject:row];
        }
        
        [array addObject:section];
    }

    
    [self.wlclass_collection_view setAllSection:array];
}
#pragma mark -- 懒加载
-(WLCollectionView *)wlclass_collection_view{
    if (!_wlclass_collection_view){
        _wlclass_collection_view = [WLCollectionView initWithVerticalDelegate:self];
        _wlclass_collection_view.backgroundColor = UIColor.blueColor;
    }
    return _wlclass_collection_view;
}

Cell.h 代码

#import 

@interface ItemCollectionViewCell : UICollectionViewCell

@end

@interface ItemCollectionViewCellModel : NSObject
@property (nonatomic,strong) NSString *string;
@end

Cell.m 代码

#import "ItemCollectionViewCell.h"
#import "WLBaseCollectionViewLayerRow.h"

@interface ItemCollectionViewCell()
@property (strong,nonatomic) UILabel *title_label;
@end

@implementation ItemCollectionViewCell
/** 绑定数据 */
-(void)bind_row_data:(WLBaseCollectionViewLayerRow *)sender{
    if ([sender.data isKindOfClass:ItemCollectionViewCellModel.class]){
        ItemCollectionViewCellModel *model = sender.data;
        NSLog(@"%@", model.string);
        self.title_label.text = model.string;
    }
}
/** 绑定代理 */
-(void)bind_delegate:(id)sender{
    
}


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.orangeColor;
        self.title_label = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, frame.size.width - 10, frame.size.height - 10)];
        self.title_label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.title_label];
    }
    return self;
}

@end


@implementation ItemCollectionViewCellModel

@end

头视图代码.h

#import 


@interface HeaderCollectionReusableView : UICollectionReusableView

@end

头视图代码.m

#import "HeaderCollectionReusableView.h"
#import "WLBaseCollectionReusableModel.h"
@interface HeaderCollectionReusableView()
@property (strong,nonatomic) UILabel *title_label;
@end

@implementation HeaderCollectionReusableView
/** 绑定数据 */
-(void)bind_row_data:(WLBaseCollectionReusableModel *)sender{
    if ([sender.data isKindOfClass:NSDictionary.class]){
        NSDictionary *model = sender.data;
        self.title_label.text = model[@"title"];
    }
}
/** 绑定代理 */
-(void)bind_delegate:(id)sender{
    
}


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.redColor;
        self.title_label = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, frame.size.width - 10, frame.size.height - 10)];
        self.title_label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.title_label];
    }
    return self;
}
@end

脚视图代码.h

#import 

@interface FootCollectionReusableView : UICollectionReusableView

@end

脚视图代码.m

#import "FootCollectionReusableView.h"
#import "WLBaseCollectionReusableModel.h"
@interface FootCollectionReusableView()
@property (strong,nonatomic) UILabel *title_label;
@end

@implementation FootCollectionReusableView
/** 绑定数据 */
-(void)bind_row_data:(WLBaseCollectionReusableModel *)sender{
    if ([sender.data isKindOfClass:NSDictionary.class]){
        NSDictionary *model = sender.data;
        self.title_label.text = model[@"title"];
    }
}
/** 绑定代理 */
-(void)bind_delegate:(id)sender{
    
}


- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.greenColor;
        self.title_label = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, frame.size.width - 10, frame.size.height - 10)];
        self.title_label.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.title_label];
    }
    return self;
}
@end

目前为止公开的代理,代理均可不实现


@protocol WLCollectionDelegate 
@required

@optional
//cell创建
-(UICollectionViewCell *)wl_collectionView:(UICollectionView *)collectionView
                                layerModel:(WLBaseCollectionViewLayerModel *)model
                             cellIndexPath:(NSIndexPath *)indexPath;

//头脚视图创建
- (UICollectionReusableView *)wl_collectionView:(UICollectionView *)collectionView
              viewForSupplementaryElementOfKind:(NSString *)kind
                              layersectionModel:(id)section
                                    atIndexPath:(NSIndexPath *)indexPath;
//点击cell的响应
- (void)wl_collectionView:(UICollectionView *)collectionView
               layerModel:(WLBaseCollectionViewLayerModel *)layerModel
             didIndexPath:(NSIndexPath *)indexPath;
- (void)wl_collectionView:(UICollectionView *)collectionView
               layerModel:(WLBaseCollectionViewLayerModel *)layerModel
             layerRowData:(id)rowData
             didIndexPath:(NSIndexPath *)indexPath;

// 结束拖拽时触发
- (void)wl_scrollViewDidEndDragging:(UIScrollView *)scrollView  willDecelerate:(BOOL)decelerate;

// 换页
- (void)wl_changeDragging:(UIScrollView *)scrollView pageNumber:(NSInteger)pageNumber;

// 滚动就会触发
- (void)wl_scrollViewDidScroll:(UIScrollView *)scrollView;

#pragma mark --缺省页
/** 自定义显示视图 */
- (UIView *)wl_customViewForWLEmptyDataSet;
/** 强制显示空数据集:当项目数量大于0时,请求代理是否仍应显示空数据集。(默认值为NO) */
- (BOOL)wl_emptyDataSetShouldBeForcedToDisplay;
/** 点击刷新按钮 */
- (void )wl_clickReloadButton;
@end

未来功能

还有未完善的UITableView,iCarousel等组件,等待完善后在添加
因为平常用的相对较少,功能还不够完善,等完善后在添加

你可能感兴趣的:(基于MVVM框架简洁性而封装的模型驱动UIViewCollecionView组件库)