用xib创建uicollectionView

#import "AspectCell.h"

#import "AspectCollectionViewCell.h"

@interface AspectCell () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

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

    }



}

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容