基于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 <UIKit/UIKit.h>

@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 <UIKit/UIKit.h>


@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 <UIKit/UIKit.h>

@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 <NSObject>
@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等组件,等待完善后在添加
因为平常用的相对较少,功能还不够完善,等完善后在添加
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,519评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,842评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,544评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,742评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,646评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,027评论 1 275
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,513评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,169评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,324评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,268评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,299评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,996评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,591评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,667评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,911评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,288评论 2 345
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,871评论 2 341