用xib创建uicollectionView

#import "AspectCell.h"

#import "AspectCollectionViewCell.h"

@interface AspectCell ()

@end

@implementation AspectCell

- (void)awakeFromNib {

    [super awakeFromNib];

    self.backgroundColor = [UIColor whiteColor];


    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];

    layout.minimumInteritemSpacing = 0;

    layout.minimumLineSpacing = 0;

    layout.itemSize=CGSizeMake(190,90);

    layout.sectionInset=UIEdgeInsetsMake(0,0,0,0);

    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;//横行滚动

    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 90) collectionViewLayout:layout];

    _collectionView.delegate = self;

    _collectionView.dataSource = self;

    _collectionView.showsVerticalScrollIndicator = NO;

    _collectionView.showsHorizontalScrollIndicator = NO;

    [_collectionView setBackgroundColor:[UIColor whiteColor]];


    //注册cell

    [_collectionView registerNib:[UINib nibWithNibName:@"AspectCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"XibCell"];

//    [_collectionView registerClass:[AspectCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];

    [self.contentView addSubview:self.collectionView];


}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [supersetSelected:selectedanimated:animated];

    // Configure the view for the selected state

}

#pragma mark -- Collection delegate

- (NSInteger)collectionView:(UICollectionView*)collectionView numberOfItemsInSection:(NSInteger)section {


    return self.collectDataArray.count;

}

- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath {


    AspectCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: @"XibCell" forIndexPath:indexPath];

    AspectModel *model=self.collectDataArray[indexPath.row];


    cell.titleLab.text=model.title;

    cell.timeLab.text=model.time;

    cell.pageViewLab.text=model.pageviews;


//    cell.backgroundColor = [UIColor clearColor];

//    cell.textLabel.text = self.collectDataArray[indexPath.row];

//    cell.imageView.image = [UIImage imageNamed:@"MyTask@2x"];

    returncell;

}

#pragma mark -- Collection delegate

- (void)collectionView:(UICollectionView*)collectionView didSelectItemAtIndexPath:(NSIndexPath*)indexPath {

    NSLog(@"点击了 %ld ", indexPath.row);


    if([self.delegaterespondsToSelector:@selector(CustomCollection:didSelectRowAtIndexPath:str:)]){

        [self.delegateCustomCollection:collectionViewdidSelectRowAtIndexPath:indexPathstr:self.collectDataArray[indexPath.row]];

    }



}

你可能感兴趣的:(用xib创建uicollectionView)