CKComponentKit CollectionView问题

时间:2015-06-09 21:31:49

标签: ios objective-c uicollectionview uicollectionviewcell componentkit

我目前正在尝试使用ComponentKit,我遇到了问题。我想使用我在主视图上创建的CKCollectionView。当我在我的MainView(包含ScrollView)中将CKCollectionView放到其容器中时,我遇到了2个问题。第一个,如果我手动创建CollectionView的框架,这里它给了我。(蓝色方块是" UICollectionViewControllerWrapperView")

UICollectionViewControllerWrapperView

但是,我真正想做的是使用砌体和约束设置框架,允许我将框架设置为等于其容器(黄色方框)。但是当我这样做时,它会用空单元格填充整个蓝色矩形高度,如下图所示:

Issue with Constraints

所以看起来我的实际单元格(或组件)的大小为647并且它们引起了问题。但是此时我迷失了,并且想知道是否有人之前有过类似的问题:)。

这是我的代码:

创建单元格的实际组件:

@implementation SSOUpcomingAuctionComponent


+ (instancetype)newWithProduct:(Product *)product {

    SSOProductHeaderComponent *head = [SSOProductHeaderComponent newWithTitle:product.time];
    SSOProductPictureComponent *body = [SSOProductPictureComponent newWithImage:product.image];
    SSOProductShareComponent *footer = [SSOProductShareComponent newWithPrice:product.price];

    return [super newWithComponent:[CKInsetComponent newWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)
                                                         component:[CKStackLayoutComponent newWithView:{
                                                             [UIView class], {
                                                                 { @selector(setBackgroundColor:), [UIColor whiteColor] }
                                                                 , { CKComponentViewAttribute::LayerAttribute(@selector(setCornerRadius:)), @6.0 }
                                                                 , { @selector(setClipsToBounds:), @YES }
                                                             }
                                                         } size:{}
                                                        style:{
                                                             .alignItems = CKStackLayoutAlignItemsCenter, .direction = CKStackLayoutDirectionVertical,
                                                             .spacing = 2
                                                         } children:{{
                                                                         head,
                                                                     },
                                                                     {body},
                                                                     {footer}}]]];
}

#pragma mark - ProviderMethod

@end

CollectionViewController:

#import "UpcomingAuctionViewController.h"
#import "SSOUpcomingAuctionComponent.h"
#import <ComponentKit/ComponentKit.h>
#import "Product.h"
#import "ProductPages.h"
#import "ProductModelController.h"
#import <ChameleonFramework/Chameleon.h>
#import "Masonry.h"

@interface UpcomingAuctionViewController () <CKComponentProvider, UICollectionViewDelegateFlowLayout>
@end

@implementation UpcomingAuctionViewController {
    CKCollectionViewDataSource *_dataSource;
    CKComponentFlexibleSizeRangeProvider *_sizeRangeProvider;
    ProductModelController *_productModelController;
}

- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout {
    if (self = [super initWithCollectionViewLayout:layout]) {
        _sizeRangeProvider = [CKComponentFlexibleSizeRangeProvider providerWithFlexibility:CKComponentSizeRangeFlexibleWidth];
        _productModelController = [[ProductModelController alloc] init];
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.collectionView.backgroundColor = [UIColor flatWhiteColorDark];
//    [self.collectionView setFrame:CGRectMake(0, 0, 320, 180)];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
          make.edges.equalTo(self.view);
        }];
    [self.collectionView setScrollEnabled:YES];

    self.collectionView.delegate = self;

    // Creates and sets our dataSource to our CKCollectionViewDataSource, THE MAIN ACTOR/INFRASTRUCTURE that takes our MODEL, creates a COMPONENT STACKS then
    // transforms it into a VIEW HIERARCHY.
    _dataSource = [[CKCollectionViewDataSource alloc] initWithCollectionView:self.collectionView
                                                 supplementaryViewDataSource:nil
                                                           componentProvider:[self class]
                                                                     context:nil
                                                   cellConfigurationFunction:nil];

    // The following block of code adds a section at 0 and two items at 0 and 1.
    CKArrayControllerSections sections;
    // insert section 0
    sections.insert(0);
    [_dataSource enqueueChangeset:{
        sections, {}
    } constrainedSize:{}];
    [self enqueueProductPages:[_productModelController fetchNewUpcomingProductsWithCount:14]];
}

- (void)enqueueProductPages:(ProductPages *)productPage {

    NSArray *products = productPage.products;
    NSInteger position = productPage.position;

    CKArrayControllerInputItems items;

    for (NSInteger i = 0; i < products.count; i++) {
        items.insert([NSIndexPath indexPathForRow:position + i inSection:0], products[i]);
    }
    [_dataSource enqueueChangeset:{
        {}
        , items
    } constrainedSize:[_sizeRangeProvider sizeRangeForBoundingSize:self.collectionView.bounds.size]];
}

#pragma mark - CKComponentProvider

// Method that our componentProvider class NEED to implement
+ (CKComponent *)componentForModel:(Product *)product context:(Product *)context {
    return [SSOUpcomingAuctionComponent newWithProduct:product];
}

#pragma mark - UICollectionViewDelegateFlowlayout

- (CGSize)collectionView:(UICollectionView *)collectionView
                  layout:(UICollectionViewLayout *)collectionViewLayout
  sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return [_dataSource sizeForItemAtIndexPath:indexPath];
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    [_dataSource announceWillAppearForItemInCell:cell];
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    [_dataSource announceDidDisappearForItemInCell:cell];
}

最后是mainViewController:

/**
 *  Initialize upcoming auctions container controller
 */
- (void)initializeUpcomingAuctionsContainerView {

    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    [flowLayout setMinimumInteritemSpacing:0];
    [flowLayout setMinimumLineSpacing:10];
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 0);

    self.upcomingAuctionsVC = [[UpcomingAuctionViewController alloc] initWithCollectionViewLayout:flowLayout];

    [self addChildViewController:self.upcomingAuctionsVC];

    [self.bottomView addSubview:self.upcomingAuctionsVC.view];

    [self.upcomingAuctionsVC didMoveToParentViewController:self];
}

@end

感谢大家的帮助,祝你有个美好的一天。

0 个答案:

没有答案