iOS:链式编程-collectionView组件化

2019-10-28  本文已影响0人  豆浆油条cc

链式编程-tableview组件化:
https://github.com/qw9685/ccTableView.git

链式编程-collectionView组件化:
https://github.com/qw9685/ccCollectionView.git

Untitled.gif

最简单的实现:

-(ccCollectionView *)collectionView{
        if (_collectionView == nil) {
            _collectionView = [[ccCollectionView alloc] initCollectionViewWithItemClass:[UICollectionViewCell class] headClass:nil footClass:nil];
            _collectionView.layout.sectionInset = UIEdgeInsetsMake(15, 15, 30, 15);
            _collectionView.layout.itemSize = CGSizeMake(100, 100);
            _collectionView.layout.minimumInteritemSpacing = 15;
            _collectionView.layout.minimumLineSpacing = 15;
            _collectionView.cc_CollectionDidSelectRowAtIndexPath(^(NSIndexPath * _Nonnull indexPath, UICollectionView * _Nonnull collectionView) {
                
                [self.navigationController pushViewController:[firstViewController new] animated:YES];
                
            }).cc_CollectionNumberOfRows(^NSInteger(NSInteger section, UICollectionView * _Nonnull collectionView) {
                
                return 5;
                
            }).cc_CollectionViewForCell(^UICollectionViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath, UICollectionView * _Nonnull collectionView) {
                
                UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([UICollectionViewCell class]) forIndexPath:indexPath];
                
                UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
                label.text = @"自定义cell";
                label.backgroundColor = [UIColor redColor];
                [cell addSubview:label];
                return cell;
            });
        }
        
        return _collectionView;
}

自定义cell,header,footer

-(ccCollectionView *)collectionView{
        if (_collectionView == nil) {
            _collectionView = [[ccCollectionView alloc] initCollectionViewWithItemClass:[firstCollectionViewCell class] headClass:[firstCollectionReusableHeadView class] footClass:[firstCollectionReusableFootView class]];
            _collectionView.layout.headerReferenceSize = CGSizeMake(500, 66);
            _collectionView.layout.footerReferenceSize = CGSizeMake(500, 10);
            
            _collectionView.cc_sizeForItemAtIndexPath(^CGSize(UICollectionViewLayout * _Nonnull layout, NSIndexPath * _Nonnull indexPath) {
                
                return CGSizeMake(100, 110*2);
                
            }).cc_CollectionDidSelectRowAtIndexPath(^(NSIndexPath * _Nonnull indexPath, UICollectionView * _Nonnull collectionView) {
                
            }).cc_CollectionviewForElementOfKind(^UICollectionReusableView * _Nonnull(NSIndexPath * _Nonnull indexPath, NSString * _Nonnull kind, UICollectionView * _Nonnull collectionView) {
                //头部
               if ([kind isEqualToString: UICollectionElementKindSectionHeader]) {
                   firstCollectionReusableHeadView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier: NSStringFromClass([firstCollectionReusableHeadView class]) forIndexPath:indexPath];
                   view.backgroundColor = [UIColor yellowColor];
                return view;
               }else{
                    firstCollectionReusableFootView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier: NSStringFromClass([firstCollectionReusableFootView class]) forIndexPath:indexPath];
                    view.backgroundColor = [UIColor blackColor];
                   return view;
               }
            }).cc_CollectionNumberOfSections(^NSInteger(UICollectionView * _Nonnull collectionView) {
                return 3;
            }).cc_CollectionNumberOfRows(^NSInteger(NSInteger section, UICollectionView * _Nonnull collectionView) {
                return 5;
            }).cc_CollectionViewForCell(^UICollectionViewCell * _Nonnull(NSIndexPath * _Nonnull indexPath, UICollectionView * _Nonnull collectionView) {
                firstCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([firstCollectionViewCell class]) forIndexPath:indexPath];
                cell.backgroundColor = [UIColor blueColor];
                return cell;
            });
        }
        
        return _collectionView;
}
上一篇 下一篇

猜你喜欢

热点阅读