FJCollectionView 0.1.2

FJCollectionView 0.1.2

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布最后发布2017年8月

jeff_njut 维护。



 
依赖关系
Masonry>= 0
CHTCollectionViewWaterfallLayout>= 0
DDCollectionViewFlowLayout>= 0
FJTool>= 0
 

  • 作者:
  • jeff_njut

如何开始

FJCollectView 是一个自定义的数据驱动的CollectionView。

Podfile

要使用 CocoaPods 将 FJCollectionView 集成到您的 Xcode 项目中,请在您的 Podfile 中指定它

pod 'FJCollectionView', :git => 'https://github.com/jeffnjut/FJCollectionView.git'

然后,运行以下命令:

$ pod install

如果发生任何更新,请运行以下命令:

$ pod update

示例

导入

导入 FJCollectionView 头文件

#import <FJCollectionView/FJCollectionViewHeader.h>

用法

快速集成 FJCollectionView

FJCollectionView *collectionView = [FJCollectionView FJCollectionView:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)
                                                              bgColor:[UIColor whiteColor]
                                                         sectionInset:UIEdgeInsetsMake(5, 5, 5, 5)
                                                          columnSpace:5
                                                       interItemSpace:10
                                                         headerHeight:0
                                                         footerHeight:0
                                                      registerClasses:@[[SampleCollectionCell class],[SampleHeaderView class],[SampleFooterView class]]
                                                            columnCnt:1
                                                               sticky:YES];

collectionView.allowCellMoved = YES;

collectionView.disableRefreshingHeader = YES;

[self.view addSubview:tableView];

[self loadData];

创建单元格 & 数据源

单元格头部

@interface SampleCollectionCell : FJCollectionCell

@end

@interface SampleCollectionCellDataSource : FJCollectionCellDataSource

@property (nonatomic , strong) UIColor *color;
@property (nonatomic , copy) NSString *txt;

@end

@interface SampleSingleCollectionCell : FJCollectionCell

@end

@interface SampleSingleCollectionCellDataSource : FJCollectionCellDataSource

@property (nonatomic , strong) UIColor *color;
@property (nonatomic , copy) NSString *txt;

@end

单元格编码

@interface SampleCollectionCell()
@property (nonatomic, weak) IBOutlet UILabel *lb_txt;
@property (nonatomic, weak) IBOutlet UIButton *btn_test;

@end

@implementation SampleCollectionCell

- (void)awakeFromNib {

    [super awakeFromNib];
    // Initialization code
    __weak typeof(self) weakSelf = self;
    [self.btn_test bk_whenTapped:^{

        SampleCollectionCellDataSource *ds = weakSelf.cellDataSource;
        weakSelf.delegate == nil ? : [weakSelf.delegate fjcell_actionRespond:ds from:weakSelf];

    }];

}

- (void)setCellDataSource:(__kindof FJCollectionCellDataSource *)cellDataSource {
    [super setCellDataSource:cellDataSource];
    SampleCollectionCellDataSource *ds = cellDataSource;
    if (ds.color) {
        self.backgroundColor = ds.color;
    }

    if (ds.txt.length > 0) {
        self.lb_txt.text = ds.txt;
    }

}

@end

@implementation SampleCollectionCellDataSource

- (instancetype)init {
    self = [super init];
    if (self) {
        self.size = CGSizeMake([[UIScreen mainScreen] bounds].size.width, arc4random() % 300 + 100.0 );
    }
    return self;
}

@end

@interface SampleSingleCollectionCell()
@property (nonatomic, weak) IBOutlet UILabel *lb_txt;
@property (nonatomic, weak) IBOutlet UIButton *btn_test;

@end

@implementation SampleSingleCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    __weak typeof(self) weakSelf = self;
    [self.btn_test bk_whenTapped:^{

        SampleSingleCollectionCellDataSource *ds = weakSelf.cellDataSource;
        weakSelf.delegate == nil ? : [weakSelf.delegate fjcell_actionRespond:ds from:weakSelf];

    }];
}

- (void)setCellDataSource:(__kindof FJCollectionCellDataSource *)cellDataSource {
    [super setCellDataSource:cellDataSource];
    SampleSingleCollectionCellDataSource *ds = cellDataSource;
    if (ds.color) {
        self.backgroundColor = ds.color;
    }

    if (ds.txt.length > 0) {
        self.lb_txt.text = ds.txt;
    }
}

@end


@implementation SampleSingleCollectionCellDataSource

- (instancetype)init {
    self = [super init];
    if (self) {
        self.size = CGSizeMake([[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.width * 1.5);
    }
    return self;
}

@end

创建分区 & 数据源

分区头部

@interface SampleHeaderView : FJCollectionHeaderView

@end

@interface SampleHeaderViewDataSource : FJCollectionHeaderViewDataSource

@end

@interface SampleFooterView : FJCollectionFooterView

@end

@interface SampleFooterViewDataSource : FJCollectionFooterViewDataSource

@end

分区编码

@interface SampleHeaderView()

@property (nonatomic , weak) IBOutlet UIButton *btn_test;

@end

@implementation SampleHeaderView

- (void)awakeFromNib {
    [super awakeFromNib];

    // Initialization code
    __weak typeof(self) weakSelf = self;
    [self.btn_test bk_whenTapped:^{
        SampleHeaderViewDataSource *ds = weakSelf.headerDataSource;
        weakSelf.delegate == nil ? : [weakSelf.delegate fjheader_actionRespond:ds from:weakSelf];
    }];
}

- (void)setHeaderDataSource:(__kindof FJCollectionHeaderAndFooterViewDataSource *)headerDataSource {
    [super setHeaderDataSource:headerDataSource];
}


@end


@implementation SampleHeaderViewDataSource

- (instancetype)init {
    self = [super init];
    if (self) {
        self.viewHeight = 200.0;
    }
    return self;
}

@end

@interface SampleFooterView()

@property (nonatomic ,weak) IBOutlet UIButton *btn_test;

@end

@implementation SampleFooterView

- (void)awakeFromNib {
    [super awakeFromNib];

    __weak typeof(self) weakSelf = self;
    [self.btn_test bk_whenTapped:^{
        SampleFooterViewDataSource *ds = weakSelf.headerDataSource;
        weakSelf.delegate == nil ? : [weakSelf.delegate fjheader_actionRespond:ds from:weakSelf];
    }];
}

- (void)setHeaderDataSource:(__kindof FJCollectionHeaderViewDataSource *)headerDataSource {
    [super setHeaderDataSource:headerDataSource];
}

@end

@implementation SampleFooterViewDataSource

- (instancetype)init {
    self = [super init];
    if (self) {
        self.viewHeight = 30.0;
    }
    return self;
}

@end

开始加载数据 & 渲染 UI

- (void)loadData {

    // Request Data

    // Render UI
    // [self renderUI];
    // [self renderMUI];
}

- (void)renderUI {
    for (int i = 0; i < 10; i++) {
        SampleCollectionCellDataSource *ds = [[SampleCollectionCellDataSource alloc] init];
        ds.color = [UIColor redColor];
        [self.collectionView addDataSource:ds];

        ds = [[SampleCollectionCellDataSource alloc] init];
        ds.color = [UIColor blueColor];
        [self.collectionView addDataSource:ds];

        ds = [[SampleCollectionCellDataSource alloc] init];
        ds.color = [UIColor orangeColor];
        [self.collectionView addDataSource:ds];

        ds = [[SampleCollectionCellDataSource alloc] init];
        ds.color = [UIColor greenColor];
        [self.collectionView addDataSource:ds];
    }
    [self.collectionView refresh];
}

创建带有分区头部的CollectionView

- (void)renderMUI {

    // Single
    FJClMultiDataSource *multiDataSource = [[FJClMultiDataSource alloc] init];
    multiDataSource.cellDataSources = [NSMutableArray array];

    SampleHeaderViewDataSource *header_ds = [[SampleHeaderViewDataSource alloc] init];
    multiDataSource.headerViewDataSource = header_ds;

    SampleFooterViewDataSource *footer_ds = [[SampleFooterViewDataSource alloc] init];
    multiDataSource.footerViewDataSource = footer_ds;

    for (int i = 0; i < 10; i++) {
        SampleCollectionCellDataSource *ds = [[SampleCollectionCellDataSource alloc] init];
        ds.color = [UIColor redColor];
        [multiDataSource.cellDataSources addObject:ds];

        ds = [[SampleCollectionCellDataSource alloc] init];
        ds.color = [UIColor blueColor];
        [multiDataSource.cellDataSources addObject:ds];

        ds = [[SampleCollectionCellDataSource alloc] init];
        ds.color = [UIColor orangeColor];
        [multiDataSource.cellDataSources addObject:ds];

        ds = [[SampleCollectionCellDataSource alloc] init];
        ds.color = [UIColor greenColor];
        [multiDataSource.cellDataSources addObject:ds];
    }

    [self.dual_dataSource addObject:multiDataSource];
    [self.collectionView setColumnCnt:2];
    [self.collectionView setDataSource:self.dual_dataSource];

    // prepare for single
    multiDataSource = [[FJClMultiDataSource alloc] init];
    multiDataSource.cellDataSources = [NSMutableArray array];

    multiDataSource.headerViewDataSource = header_ds;
    multiDataSource.footerViewDataSource = footer_ds;

    for (int i = 0; i < 10; i++) {
        SampleSingleCollectionCellDataSource *ds = [[SampleSingleCollectionCellDataSource alloc] init];
        ds.color = [UIColor redColor];
        [multiDataSource.cellDataSources addObject:ds];

        ds = [[SampleSingleCollectionCellDataSource alloc] init];
        ds.color = [UIColor blueColor];
        [multiDataSource.cellDataSources addObject:ds];

        ds = [[SampleSingleCollectionCellDataSource alloc] init];
        ds.color = [UIColor orangeColor];
        [multiDataSource.cellDataSources addObject:ds];

        ds = [[SampleSingleCollectionCellDataSource alloc] init];
        ds.color = [UIColor greenColor];
        [multiDataSource.cellDataSources addObject:ds];
    }

    [self.single_dataSource addObject:multiDataSource];

}

编写处理单元格上发生的操作的块

__weak typeof(self) weakSelf = self;
[self.collectionView setCollectionCellActionBlock:^(FJ_CollectionCellBlockType type, NSInteger item, NSInteger section, FJCollectionCellDataSource *cellData) {
    switch (type) {
        case FJ_CollectionCellBlockType_CellTapped:
        {
            NSLog(@"fj_didSelectItem : %d", (int)item);
            break;
        }

        case FJ_CollectionCellBlockType_CellCustomizedTapped:
        {
            if ([cellData isKindOfClass:[SampleHeaderViewDataSource class]]) {
                NSLog(@"SampleHeaderViewDataSource");
                static BOOL single = NO;
                if (single) {
                    [weakSelf.collectionView setColumnCnt:2];
                    [weakSelf.collectionView setDataSource:weakSelf.dual_dataSource];
                }else{
                    [weakSelf.collectionView setColumnCnt:1];
                    [weakSelf.collectionView setDataSource:weakSelf.single_dataSource];
                }
                single = !single;

            }else if ([cellData isKindOfClass:[SampleFooterViewDataSource class]]) {
                NSLog(@"SampleFooterViewDataSource");
            }else if ([cellData isKindOfClass:[SampleCollectionCellDataSource class]]) {
                NSLog(@"SampleCollectionCellDataSource");
            }else if ([cellData isKindOfClass:[SampleSingleCollectionCellDataSource class]]) {
                NSLog(@"SampleSingleCollectionCellDataSource");
            }
            break;
        }
    }
}];

编写处理移动操作的块

[self.collectionView setCollectionCellMoveActionBlock:^(NSIndexPath *fromIndexPath, NSIndexPath *toIndexPath, FJCollectionCellDataSource *movedDataSource) {
    NSLog(@"From Index : %@ , To Index : %@ , Moved DataSource : %@", fromIndexPath, toIndexPath, movedDataSource);
}];

编写处理滚动操作的块

[self.collectionView setCollectionScrollActionBlock:^(FJ_CollectionScrollType type, UIScrollView *scrollView, CGFloat moveHeight, BOOL isUp) {
    if (isUp) {
        NSLog(@"向上滑动: %f", moveHeight);
    }else{
        NSLog(@"向下滑动: %f", moveHeight);
    }
}];

无缝实现 MJRefresh 与 FJCollectionView 的使用

[self.collectionView collectionView].mj_header = MJHeader Object ...
[self.collectionView collectionView].mj_footer = MJFooter Object ...

贡献

如有需求帮助或发现Bug,欢迎提交issue或pull request。

联系

待办

  • 文档

许可

FJCollectionView 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。

MIT 许可证 (MIT)

版权所有 © 2017 Jeff

以下是对任何获得此软件及其关联文档文件(以下简称“软件”)副本的个人授予免费、非专有、不可转让的许可,以便在不限制使用前提下,无限制地使用该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许获得该软件的个人进行使用,但必须遵守以下条件:

上述版权声明和本许可声明必须包含在任何软件副本或实质部分的软件中。

软件按“现状”提供一个,不提供任何明示或暗示的保证,包括但不限于任何关于适销性、针对特殊目的适用性和非侵权性的保证。在任何事件中,无论作者或版权所有者是否有过错,均不对任何索赔、损害或其他责任负责,包括但不限于合同行为、侵权或其他行为,这些责任是由软件本身、使用或其他方式与软件或软件的使用相关的。