項目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等組件柄延,等待完善后在添加
因為平常用的相對較少,功能還不夠完善映之,等完善后在添加