Latest 0.0.1
Homepage https://github.com/jeffnjut/FJCollectionView
License MIT
Platforms ios 7.0, requires ARC
Dependencies Masonry, CHTCollectionViewWaterfallLayout, DDCollectionViewFlowLayout, FJTool
Authors

How To Get Started

FJCollectView is a customized data-driven collectionview.

Installation with CocoaPods

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries like FJCollectionView in your projects. See the Get Started section for more details.

Podfile

To integrate FJCollectionView into your Xcode project using CocoaPods, specify it in your Podfile:

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

Then, run the following command:

$ pod install

If any update occurs, run the following command:

$ pod update

Demo

Import

Import FJCollectionView Header File

#import <FJCollectionView/FJCollectionViewHeader.h>

Usage

Fast Involve 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];

Make Cell & DataSource

Cell Header

@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

Cell Coding

@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

Make Section & DataSource

Section Header

@interface SampleHeaderView : FJCollectionHeaderView

@end

@interface SampleHeaderViewDataSource : FJCollectionHeaderViewDataSource

@end

@interface SampleFooterView : FJCollectionFooterView

@end

@interface SampleFooterViewDataSource : FJCollectionFooterViewDataSource

@end

Section Coding

@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

Start Loading Data & Rendering 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];
}

Write A CollectionView With Section Header

- (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];

}

Write Block for Dealing with Action Occurs on Cell

__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;
        }
    }
}];

Write Block for Dealing with Moving Action

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

Write Block for Scrolling Action

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

Write MJRefresh with FJCollectionView seamlessly

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

Contribute

Feel free to open an issue or pull request, if you need help or there is a bug.

Contact

Todo

  • Documentation

License

FJCollectionView is available under the MIT license. See the LICENSE file for more info.

The MIT License (MIT)

Copyright (c) 2017 Jeff

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Latest podspec

{
    "name": "FJCollectionView",
    "version": "0.0.1",
    "summary": "u3010FJCollectionViewu3011u81eau5b9au4e49Data Drivenu7684CollectionViewuff0cu652fu6301HeaderView Sticky",
    "homepage": "https://github.com/jeffnjut/FJCollectionView",
    "license": "MIT",
    "authors": {
        "jeff_njut": "[email protected]"
    },
    "platforms": {
        "ios": "7.0"
    },
    "source": {
        "git": "https://github.com/jeffnjut/FJCollectionView.git",
        "tag": "0.0.1"
    },
    "requires_arc": true,
    "source_files": "FJCollectionView/classes/**/*.{h,m}",
    "dependencies": {
        "Masonry": [],
        "CHTCollectionViewWaterfallLayout": [],
        "DDCollectionViewFlowLayout": [],
        "FJTool": []
    }
}

Pin It on Pinterest

Share This