基于MVVM框架簡潔性而封裝的模型驅(qū)動UIViewCollecionView組件庫

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

優(yōu)勢

更加符合MVVM框架
由VM生成CollectionView的顯示視圖模型蜘醋,回調(diào)給View中直接進行渲染
在項目中可在絕大多數(shù)情況下避免數(shù)組越界胁塞,cell初始化問題等崩潰
快速開發(fā)利器小幫手
可無縫接入項目,無需修改已存在Cell的內(nèi)容压语,只需添加兩個方法
一個數(shù)據(jù)綁定方法啸罢,一個代理綁定方法

使用方式

pod 'WLCollectionView' , '~> 1.0.1'
或者
下載源碼中的Class文件下內(nèi)容拖入項目,相對cocopods導(dǎo)入胎食,可做定制化功能

項目核心源碼扰才,相對簡單,全部都有注釋厕怜,并且無依賴性衩匣,可拆解使用

核心消息方法轉(zhuǎn)發(fā)分類在 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
/** 綁定數(shù)據(jù) */
-(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
/** 綁定數(shù)據(jù) */
-(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
/** 綁定數(shù)據(jù) */
-(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

目前為止公開的代理粥航,代理均可不實現(xiàn)


@protocol WLCollectionDelegate <NSObject>
@required

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

//頭腳視圖創(chuàng)建
- (UICollectionReusableView *)wl_collectionView:(UICollectionView *)collectionView
              viewForSupplementaryElementOfKind:(NSString *)kind
                              layersectionModel:(id)section
                                    atIndexPath:(NSIndexPath *)indexPath;
//點擊cell的響應(yīng)
- (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;

// 結(jié)束拖拽時觸發(fā)
- (void)wl_scrollViewDidEndDragging:(UIScrollView *)scrollView  willDecelerate:(BOOL)decelerate;

// 換頁
- (void)wl_changeDragging:(UIScrollView *)scrollView pageNumber:(NSInteger)pageNumber;

// 滾動就會觸發(fā)
- (void)wl_scrollViewDidScroll:(UIScrollView *)scrollView;

#pragma mark --缺省頁
/** 自定義顯示視圖 */
- (UIView *)wl_customViewForWLEmptyDataSet;
/** 強制顯示空數(shù)據(jù)集:當(dāng)項目數(shù)量大于0時琅捏,請求代理是否仍應(yīng)顯示空數(shù)據(jù)集。(默認(rèn)值為NO) */
- (BOOL)wl_emptyDataSetShouldBeForcedToDisplay;
/** 點擊刷新按鈕 */
- (void )wl_clickReloadButton;
@end

未來功能

還有未完善的UITableView递雀,iCarousel等組件柄延,等待完善后在添加
因為平常用的相對較少,功能還不夠完善映之,等完善后在添加
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末拦焚,一起剝皮案震驚了整個濱河市蜡坊,隨后出現(xiàn)的幾起案子杠输,更是在濱河造成了極大的恐慌赎败,老刑警劉巖,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件蠢甲,死亡現(xiàn)場離奇詭異僵刮,居然都是意外死亡,警方通過查閱死者的電腦和手機鹦牛,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門搞糕,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人曼追,你說我怎么就攤上這事窍仰。” “怎么了礼殊?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵驹吮,是天一觀的道長慧邮。 經(jīng)常有香客問我怎囚,道長贮喧,這世上最難降的妖魔是什么霹疫? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任哨免,我火速辦了婚禮找御,結(jié)果婚禮上误辑,老公的妹妹穿的比我還像新娘旋炒。我一直安慰自己泌参,他們只是感情好脆淹,可當(dāng)我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著沽一,像睡著了一般未辆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上锯玛,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天咐柜,我揣著相機與錄音,去河邊找鬼攘残。 笑死拙友,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的歼郭。 我是一名探鬼主播遗契,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼病曾!你這毒婦竟也來了牍蜂?” 一聲冷哼從身側(cè)響起漾根,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎鲫竞,沒想到半個月后辐怕,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡从绘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年寄疏,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片僵井。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡陕截,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出批什,到底是詐尸還是另有隱情农曲,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布驻债,位于F島的核電站乳规,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏却汉。R本人自食惡果不足惜驯妄,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望合砂。 院中可真熱鬧青扔,春花似錦、人聲如沸翩伪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽缘屹。三九已至凛剥,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間轻姿,已是汗流浹背犁珠。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留互亮,地道東北人犁享。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像豹休,于是被迫代替她去往敵國和親炊昆。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,044評論 2 355